Line data Source code
1 : #include <time.h>
2 : #include <stdio.h>
3 : #include <string.h>
4 : #include <unistd.h>
5 : #include "fifo.h"
6 : #include "log.h"
7 : #include <pthread.h>
8 :
9 : static struct {
10 : int verbose;
11 : netio_tag_t fid;
12 : int error_fd;
13 : } log_config;
14 :
15 :
16 : static struct {
17 : char hostname_long[256];
18 : char* hostname;
19 : int pid;
20 : char cwd[256];
21 : int uid;
22 : char uname[256];
23 : char appname[256];
24 : int device;
25 : } context_info;
26 :
27 83 : void log_init(int verbose, char* error_fifo, netio_tag_t fid, const char* appname, int device)
28 : {
29 83 : log_config.verbose = verbose;
30 83 : log_config.fid = fid;
31 83 : log_config.error_fd = error_fifo == NULL ? 0 : open_fifo(error_fifo);
32 :
33 83 : const char sep[2] = ".";
34 83 : int rc = gethostname(context_info.hostname_long, 256);
35 83 : if (rc < 0) {memset(context_info.hostname_long, '\0', 256);}
36 83 : else {context_info.hostname = strtok(context_info.hostname_long, sep);}
37 83 : context_info.pid = (int) getpid();
38 83 : getcwd(context_info.cwd, sizeof(context_info.cwd));
39 83 : context_info.uid = (int) getuid();
40 83 : getlogin_r(context_info.uname, sizeof(context_info.uname));
41 83 : strcpy(context_info.appname, appname);
42 83 : context_info.device = device;
43 83 : }
44 :
45 129024 : void _log_dump(const char* level, const char* filename, unsigned line, const char* functionname, int tid, const char *format, ...)
46 : {
47 129024 : va_list args;
48 129024 : va_start(args, format);
49 :
50 129024 : char timestamp[72];
51 129024 : time_t ltime;
52 129024 : ltime=time(NULL);
53 129024 : struct tm *tm;
54 129024 : tm=localtime(<ime);
55 :
56 129024 : sprintf(timestamp,"%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year+1900, tm->tm_mon +1,
57 : tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
58 129024 : fprintf(stderr, "%s [%s] ", timestamp, level);
59 :
60 129024 : char msg[1024];
61 129024 : vsnprintf(msg, 1024, format, args);
62 129024 : fputs(msg, stderr);
63 129024 : fputs("\n", stderr);
64 129024 : fflush(stderr);
65 129024 : va_end(args);
66 129024 : }
67 :
68 166347 : void _log(const char* level, const char* filename, unsigned line, const char* functionname, int tid, const char *format, ...)
69 : {
70 166347 : if (!strcmp("DEBUG", level) && !log_config.verbose) return;
71 :
72 165647 : va_list args;
73 165647 : va_start(args, format);
74 165647 : const char* fn = filename == NULL ? "" : filename;
75 :
76 165647 : struct ers_data ers_data = {"felix-star", fn, line, functionname, context_info.device, context_info.hostname, context_info.pid, tid, context_info.cwd, context_info.uid, context_info.uname, context_info.appname};
77 :
78 165647 : char timestamp[72];
79 165647 : time_t ltime;
80 165647 : ltime=time(NULL);
81 165647 : struct tm *tm;
82 165647 : tm=localtime(<ime);
83 :
84 165647 : sprintf(timestamp,"%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year+1900, tm->tm_mon +1,
85 : tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
86 :
87 165647 : printf("%s [%s] ", timestamp, level);
88 165647 : char msg[1024];
89 165647 : vsnprintf(msg, 1024, format, args);
90 :
91 165647 : puts(msg);
92 :
93 165647 : log_flush();
94 :
95 165647 : va_end(args);
96 :
97 165647 : if(log_config.error_fd > 0) {
98 : // json Date Format: "2012-04-23T18:25:43.511Z"
99 3839 : sprintf(timestamp,"%04d-%02d-%02dT%02d:%02d:%02d.000Z", tm->tm_year+1900, tm->tm_mon + 1,
100 : tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
101 3839 : error_write(log_config.error_fd, log_config.fid, timestamp, level, msg, &ers_data);
102 : }
103 : }
104 :
105 165896 : void log_flush()
106 : {
107 165896 : fflush(stdout);
108 249 : }
|