output.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. clsync - file tree sync utility based on fanotify and inotify
  3. Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <syslog.h>
  16. #include "common.h"
  17. #include "output.h"
  18. static int *flags;
  19. printf_funct _printf_ddd=NULL;
  20. printf_funct _printf_dd=NULL;
  21. printf_funct _printf_d=NULL;
  22. printf_funct _printf_v=NULL;
  23. printf_funct printf_e=NULL;
  24. write_funct _write_ddd=NULL;
  25. write_funct _write_dd=NULL;
  26. write_funct _write_d=NULL;
  27. write_funct _write_v=NULL;
  28. write_funct write_e=NULL;
  29. int out_init(int *flags_init) {
  30. flags = flags_init;
  31. // static char buf[OUTPUT_BUFSIZE];
  32. if(!flags[QUIET]) {
  33. if(flags[SYSLOG]) {
  34. openlog(PROGRAM, LOG_PID, LOG_DAEMON);
  35. printf_e = printf_syslog_err;
  36. write_e = write_syslog_err;
  37. if(flags[SYSLOG]>0) {
  38. _printf_d = printf_syslog_debug;
  39. _write_d = write_syslog_debug;
  40. }
  41. if(flags[SYSLOG]>1) {
  42. _printf_dd = printf_syslog_debug;
  43. _write_dd = write_syslog_debug;
  44. }
  45. if(flags[SYSLOG]>2) {
  46. _printf_ddd = printf_syslog_debug;
  47. _write_ddd = write_syslog_debug;
  48. }
  49. if(flags[VERBOSE]) {
  50. _printf_v = printf_syslog_info;
  51. _write_v = write_syslog_info;
  52. }
  53. } else {
  54. printf_e = printf_stderr;
  55. write_e = write_stderr;
  56. if(flags[DEBUG]>0) {
  57. _printf_d = printf_e;
  58. _write_d = write_e;
  59. }
  60. if(flags[DEBUG]>1) {
  61. _printf_dd = printf_e;
  62. _write_dd = write_e;
  63. }
  64. if(flags[DEBUG]>2) {
  65. _printf_ddd = printf_e;
  66. _write_ddd = write_e;
  67. }
  68. if(flags[VERBOSE]) {
  69. _printf_v = printf_e;
  70. _write_v = write_e;
  71. }
  72. }
  73. }
  74. // setvbuf(stdout, buf, _IOFBF, OUTPUT_BUFSIZE);
  75. return 0;
  76. }
  77. void out_deinit() {
  78. if(!flags[QUIET]) {
  79. if(flags[SYSLOG]) {
  80. closelog();
  81. }
  82. }
  83. }
  84. int debug_print_flags() {
  85. int flag=0;
  86. printf_d("Debug: current flags: ");
  87. while(flag < (1<<8)) {
  88. if(flags[flag]) {
  89. int i=0;
  90. while(i++ < flags[flag])
  91. printf_d("%c", flag);
  92. }
  93. flag++;
  94. }
  95. printf_d("\n");
  96. return 0;
  97. }
  98. int printf_syslog_info(const char *fmt, ...) {
  99. va_list args;
  100. va_start(args, fmt);
  101. vsyslog(LOG_INFO, fmt, args);
  102. va_end(args);
  103. return 0;
  104. }
  105. int printf_syslog_debug(const char *fmt, ...) {
  106. va_list args;
  107. va_start(args, fmt);
  108. vsyslog(LOG_DEBUG, fmt, args);
  109. va_end(args);
  110. return 0;
  111. }
  112. int printf_syslog_err(const char *fmt, ...) {
  113. va_list args;
  114. va_start(args, fmt);
  115. vsyslog(LOG_ERR, fmt, args);
  116. va_end(args);
  117. return 0;
  118. }
  119. int printf_stderr(const char *fmt, ...) {
  120. int ret_print;
  121. va_list args;
  122. va_start(args, fmt);
  123. ret_print = vfprintf(stderr, fmt, args);
  124. va_end(args);
  125. return ret_print;
  126. }
  127. int write_syslog_info(const char *buf, size_t len) {
  128. // TODO: Consider with "len"
  129. syslog(LOG_INFO, "%s", buf);
  130. return 0;
  131. }
  132. int write_syslog_debug(const char *buf, size_t len) {
  133. // TODO: Consider with "len"
  134. syslog(LOG_DEBUG, "%s", buf);
  135. return 0;
  136. }
  137. int write_syslog_err(const char *buf, size_t len) {
  138. // TODO: Consider with "len"
  139. syslog(LOG_ERR, "%s", buf);
  140. return 0;
  141. }
  142. int write_stderr(const char *buf, size_t len) {
  143. return fwrite(buf, len, 1, stderr);
  144. }
  145. int write_stdout(const char *buf, size_t len) {
  146. return fwrite(buf, len, 1, stdout);
  147. }
  148. int printf_stdout(const char *fmt, ...) {
  149. va_list args;
  150. va_start(args, fmt);
  151. int ret_print = vprintf(fmt, args);
  152. va_end(args);
  153. return ret_print;
  154. }
  155. void out_flush() {
  156. fflush(stdout);
  157. }
  158. void hexdump_e(const unsigned char *buf, size_t len) {
  159. size_t i=0;
  160. while(i<len)
  161. printf_e("%5p ", (void *)(unsigned long)buf[i++]);
  162. write_e("\n", 1);
  163. return;
  164. }
  165. void hexdump_d(const unsigned char *buf, size_t len) {
  166. if(flags[DEBUG]<1)
  167. return;
  168. hexdump_e(buf, len);
  169. }
  170. void hexdump_dd(const unsigned char *buf, size_t len) {
  171. if(flags[DEBUG]<2)
  172. return;
  173. hexdump_e(buf, len);
  174. }