File log.c changed (mode: 100644) (index c7627d9..7f72ec5) |
2 |
2 |
#include <stdio.h> |
#include <stdio.h> |
3 |
3 |
#include <stdlib.h> |
#include <stdlib.h> |
4 |
4 |
#include <string.h> |
#include <string.h> |
|
5 |
|
#include <syslog.h> |
5 |
6 |
#include <time.h> |
#include <time.h> |
6 |
7 |
|
|
7 |
8 |
#include "log.h" |
#include "log.h" |
8 |
9 |
|
|
|
10 |
|
/* |
|
11 |
|
* The minimum log level; set to one of L_* constants from log.h |
|
12 |
|
*/ |
9 |
13 |
int min_log_level = 666; |
int min_log_level = 666; |
10 |
14 |
|
|
|
15 |
|
/* |
|
16 |
|
* 0: send output to stderr |
|
17 |
|
* 1: send output to syslog LOG_LOCAL1 facility |
|
18 |
|
*/ |
|
19 |
|
int use_syslog = 0; |
|
20 |
|
|
11 |
21 |
/* Turn log level number to a printable string */ |
/* Turn log level number to a printable string */ |
12 |
22 |
char *log_printable_level(int level) |
char *log_printable_level(int level) |
13 |
23 |
{ |
{ |
|
... |
... |
char *log_printable_level(int level) |
27 |
37 |
return "UNKNOWN"; |
return "UNKNOWN"; |
28 |
38 |
} |
} |
29 |
39 |
|
|
|
40 |
|
void log_init(void) |
|
41 |
|
{ |
|
42 |
|
if(use_syslog) |
|
43 |
|
{ |
|
44 |
|
openlog("tuntox", LOG_PID, LOG_LOCAL1); |
|
45 |
|
} |
|
46 |
|
} |
|
47 |
|
|
|
48 |
|
void log_close(void) |
|
49 |
|
{ |
|
50 |
|
if(use_syslog) |
|
51 |
|
{ |
|
52 |
|
closelog(); |
|
53 |
|
} |
|
54 |
|
} |
|
55 |
|
|
30 |
56 |
/* Output the log to the console */ |
/* Output the log to the console */ |
31 |
57 |
void log_printf(int level, const char *fmt, ...) |
void log_printf(int level, const char *fmt, ...) |
32 |
58 |
{ |
{ |
|
... |
... |
void log_printf(int level, const char *fmt, ...) |
42 |
68 |
return; |
return; |
43 |
69 |
} |
} |
44 |
70 |
|
|
45 |
|
time(&rawtime); |
|
46 |
|
timeinfo = localtime(&rawtime); |
|
47 |
|
strftime(logtime, 100, "%F %X", timeinfo); |
|
|
71 |
|
if(!use_syslog) |
|
72 |
|
{ |
|
73 |
|
time(&rawtime); |
|
74 |
|
timeinfo = localtime(&rawtime); |
|
75 |
|
strftime(logtime, 100, "%F %X", timeinfo); |
48 |
76 |
|
|
49 |
|
level_str = log_printable_level(level); |
|
|
77 |
|
level_str = log_printable_level(level); |
50 |
78 |
|
|
51 |
|
if(fmt[strlen(fmt)-1] == '\n') |
|
52 |
|
{ |
|
53 |
|
snprintf(logfmt, 2048, "%s: [%s]\t%s", logtime, level_str, fmt); |
|
|
79 |
|
if(fmt[strlen(fmt)-1] == '\n') |
|
80 |
|
{ |
|
81 |
|
snprintf(logfmt, 2048, "%s: [%s]\t%s", logtime, level_str, fmt); |
|
82 |
|
} |
|
83 |
|
else |
|
84 |
|
{ |
|
85 |
|
snprintf(logfmt, 2048, "%s: [%s]\t%s\n", logtime, level_str, fmt); |
|
86 |
|
} |
|
87 |
|
|
|
88 |
|
va_start(args, fmt); |
|
89 |
|
vfprintf(stderr, logfmt, args); |
|
90 |
|
va_end(args); |
54 |
91 |
} |
} |
55 |
92 |
else |
else |
56 |
93 |
{ |
{ |
57 |
|
snprintf(logfmt, 2048, "%s: [%s]\t%s\n", logtime, level_str, fmt); |
|
|
94 |
|
vsyslog(LOG_MAKEPRI(LOG_LOCAL1, level), fmt, args); |
58 |
95 |
} |
} |
59 |
|
|
|
60 |
|
va_start(args, fmt); |
|
61 |
|
vfprintf(stderr, logfmt, args); |
|
62 |
|
va_end(args); |
|
63 |
96 |
} |
} |
64 |
97 |
|
|
65 |
98 |
|
|
File log.h changed (mode: 100644) (index 25b19f7..ed48364) |
7 |
7 |
#define L_UNSET 0x29a |
#define L_UNSET 0x29a |
8 |
8 |
|
|
9 |
9 |
void log_printf(int level, const char *fmt, ...); |
void log_printf(int level, const char *fmt, ...); |
|
10 |
|
void log_init(void); |
|
11 |
|
void log_close(void); |
10 |
12 |
|
|
11 |
13 |
extern int min_log_level; |
extern int min_log_level; |
|
14 |
|
extern int use_syslog; |
12 |
15 |
|
|
13 |
16 |
#define d(x) log_printf(L_DEBUG, "%s:%d %s", __FILE__, __LINE__, #x); |
#define d(x) log_printf(L_DEBUG, "%s:%d %s", __FILE__, __LINE__, #x); |
14 |
17 |
|
|
File main.c changed (mode: 100644) (index 9ee98ba..dae2d34) |
... |
... |
void cleanup(int status, void *tmp) |
679 |
679 |
{ |
{ |
680 |
680 |
close(client_socket); |
close(client_socket); |
681 |
681 |
} |
} |
|
682 |
|
log_close(); |
682 |
683 |
} |
} |
683 |
684 |
|
|
684 |
685 |
|
|
|
... |
... |
void help() |
778 |
779 |
fprintf(stderr, "-C <dir> - save private key in <dir> instead of /etc/tuntox in server mode\n"); |
fprintf(stderr, "-C <dir> - save private key in <dir> instead of /etc/tuntox in server mode\n"); |
779 |
780 |
fprintf(stderr, "-d - debug mode\n"); |
fprintf(stderr, "-d - debug mode\n"); |
780 |
781 |
fprintf(stderr, "-q - quiet mode\n"); |
fprintf(stderr, "-q - quiet mode\n"); |
|
782 |
|
fprintf(stderr, "-S - send output to syslog instead of stderr\n"); |
781 |
783 |
fprintf(stderr, "-h - this help message\n"); |
fprintf(stderr, "-h - this help message\n"); |
782 |
784 |
} |
} |
783 |
785 |
|
|
|
... |
... |
int main(int argc, char *argv[]) |
787 |
789 |
unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; |
unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; |
788 |
790 |
int oc; |
int oc; |
789 |
791 |
|
|
790 |
|
while ((oc = getopt(argc, argv, "L:pi:C:P:dq")) != -1) |
|
|
792 |
|
log_init(); |
|
793 |
|
|
|
794 |
|
while ((oc = getopt(argc, argv, "L:pi:C:P:dqhS")) != -1) |
791 |
795 |
{ |
{ |
792 |
796 |
switch(oc) |
switch(oc) |
793 |
797 |
{ |
{ |
|
... |
... |
int main(int argc, char *argv[]) |
851 |
855 |
case 'q': |
case 'q': |
852 |
856 |
min_log_level = L_ERROR; |
min_log_level = L_ERROR; |
853 |
857 |
break; |
break; |
|
858 |
|
case 'S': |
|
859 |
|
use_syslog = 1; |
|
860 |
|
break; |
854 |
861 |
case '?': |
case '?': |
|
862 |
|
case 'h': |
855 |
863 |
default: |
default: |
856 |
864 |
print_version(); |
print_version(); |
857 |
865 |
help(); |
help(); |