1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,139 @@ |
1 |
+/* |
|
2 |
+ * loglib.h |
|
3 |
+ * |
|
4 |
+ * A non-intrusive minimal log framework. |
|
5 |
+ * |
|
6 |
+ * History: |
|
7 |
+ * 14/03/2014 Creation |
|
8 |
+ * |
|
9 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
10 |
+ * This file is licensed under the terms of the GNU LGPL v2+ |
|
11 |
+ */ |
|
12 |
+ |
|
13 |
+#include <stdio.h> |
|
14 |
+#include <stdlib.h> |
|
15 |
+#include <unistd.h> |
|
16 |
+#include <string.h> |
|
17 |
+#include <time.h> |
|
18 |
+#include <stdarg.h> |
|
19 |
+#include <sys/types.h> |
|
20 |
+#include <sys/stat.h> |
|
21 |
+#include <fcntl.h> |
|
22 |
+ |
|
23 |
+#include "loglib.h" |
|
24 |
+ |
|
25 |
+#define MAXLOGFILENAME 1024 |
|
26 |
+#define LOGWRITEBUFSIZE 4096 |
|
27 |
+#define LOGWRITEHEXDUMPSIZE 4096 |
|
28 |
+ |
|
29 |
+#define DEFAULTFD 2 |
|
30 |
+ |
|
31 |
+void |
|
32 |
+log_write(char *type, char *format, ...) |
|
33 |
+{ |
|
34 |
+ static void (*writefunc)(int fd, char *buf, long bufsize, void *usrptr)=NULL; |
|
35 |
+ static void *usrptr=NULL; |
|
36 |
+ static int fd=DEFAULTFD; |
|
37 |
+ static char logfilename[MAXLOGFILENAME]={""}; |
|
38 |
+ static char writebuf[LOGWRITEBUFSIZE]; |
|
39 |
+ char *ptr; |
|
40 |
+ time_t t; |
|
41 |
+ struct tm stm; |
|
42 |
+ va_list arglist; |
|
43 |
+ /* if it is a special action, perform it */ |
|
44 |
+ if(type==((char *)log_setlogfile)) { |
|
45 |
+ if(fd!=DEFAULTFD) |
|
46 |
+ close(fd),fd=DEFAULTFD; |
|
47 |
+ strncpy(logfilename,format,sizeof(logfilename)-1); |
|
48 |
+ logfilename[sizeof(logfilename)-1]='\0'; |
|
49 |
+ return; |
|
50 |
+ } else if(type==((char *)log_closelogfile)) { |
|
51 |
+ if(fd!=DEFAULTFD) |
|
52 |
+ close(fd),fd=DEFAULTFD; |
|
53 |
+ return; |
|
54 |
+ } else if(type==((char *)log_setwritefunc)) { |
|
55 |
+ writefunc=(void (*)(int /*fd*/, char * /*buf*/, long /*bufsize*/, void */*usrptr*/)) format; |
|
56 |
+ va_start(arglist,format); |
|
57 |
+ usrptr=va_arg(arglist,void *); |
|
58 |
+ va_end(arglist); |
|
59 |
+ return; |
|
60 |
+ } |
|
61 |
+ /* it is a new log entry */ |
|
62 |
+ /* make sure the file is open */ |
|
63 |
+ if(fd==DEFAULTFD && logfilename[0]!='\0') { |
|
64 |
+ if((fd=open(logfilename,O_WRONLY|O_CREAT|O_APPEND,0640))==-1) { |
|
65 |
+ fd=DEFAULTFD; |
|
66 |
+ return; |
|
67 |
+ } |
|
68 |
+ } |
|
69 |
+ /* prepare the string */ |
|
70 |
+ t=time(NULL); |
|
71 |
+ localtime_r(&t,&stm); |
|
72 |
+ ptr=writebuf; |
|
73 |
+ strftime(ptr,writebuf+LOGWRITEBUFSIZE-ptr-1,"%b %e %H:%M:%S ",&stm); |
|
74 |
+ writebuf[LOGWRITEBUFSIZE-1]='\0'; |
|
75 |
+ ptr+=strlen(ptr); |
|
76 |
+ snprintf(ptr,writebuf+LOGWRITEBUFSIZE-ptr-1,"%s: ",type); |
|
77 |
+ writebuf[LOGWRITEBUFSIZE-1]='\0'; |
|
78 |
+ ptr+=strlen(ptr); |
|
79 |
+ va_start(arglist,format); |
|
80 |
+ vsnprintf(ptr,writebuf+LOGWRITEBUFSIZE-ptr-1,format,arglist); |
|
81 |
+ va_end(arglist); |
|
82 |
+ writebuf[LOGWRITEBUFSIZE-1]='\0'; |
|
83 |
+ ptr+=strlen(ptr); |
|
84 |
+ /* if the string is not terminated with a '\n', add the '\n' */ |
|
85 |
+ if(ptr>writebuf && ptr[-1]!='\n') { |
|
86 |
+ if(ptr<(writebuf+LOGWRITEBUFSIZE-1)) { |
|
87 |
+ *(ptr++)='\n'; |
|
88 |
+ *ptr='\0'; |
|
89 |
+ } else |
|
90 |
+ ptr[-1]='\n'; |
|
91 |
+ } |
|
92 |
+ /* commit the data */ |
|
93 |
+ if(writefunc!=NULL) |
|
94 |
+ writefunc(fd,writebuf,ptr-writebuf,usrptr); |
|
95 |
+ else |
|
96 |
+ write(fd,writebuf,ptr-writebuf); |
|
97 |
+} |
|
98 |
+ |
|
99 |
+char * |
|
100 |
+log_hexdumpstr(char *data, long datasize) |
|
101 |
+{ |
|
102 |
+ static char hexdumpbuf[LOGWRITEHEXDUMPSIZE]; |
|
103 |
+ char c,*ptr; |
|
104 |
+ long i,l; |
|
105 |
+ l=(datasize>(LOGWRITEHEXDUMPSIZE/3))?(LOGWRITEHEXDUMPSIZE/3):datasize; |
|
106 |
+ for(i=0,ptr=hexdumpbuf;i<l;i++) { |
|
107 |
+ c=data[i]&0xf; |
|
108 |
+ *(ptr++)=(c<0xa)?c+'0':c+'a'-10; |
|
109 |
+ c=(data[i]>>4)&0xf; |
|
110 |
+ *(ptr++)=(c<0xa)?c+'0':c+'a'-10; |
|
111 |
+ *(ptr++)=' '; |
|
112 |
+ } |
|
113 |
+ ptr[(i>0)?i-1:0]='\0'; |
|
114 |
+ return(hexdumpbuf); |
|
115 |
+} |
|
116 |
+ |
|
117 |
+void |
|
118 |
+log_setlogfile(char *filename) |
|
119 |
+{ |
|
120 |
+ log_write((char *)log_setlogfile,(char *)filename); |
|
121 |
+} |
|
122 |
+ |
|
123 |
+void |
|
124 |
+log_closelogfile(void) |
|
125 |
+{ |
|
126 |
+ void *dummy=NULL; |
|
127 |
+ log_write((char *)log_closelogfile,(char *)dummy); |
|
128 |
+} |
|
129 |
+ |
|
130 |
+ |
|
131 |
+void |
|
132 |
+log_setwritefunc(void (*newwritefunc)(int fd, char *buf, long bufsize, void *usrptr),void *usrptr) |
|
133 |
+{ |
|
134 |
+ log_write((char *)log_setwritefunc,(char *)newwritefunc,usrptr); |
|
135 |
+} |
|
136 |
+ |
|
137 |
+ |
|
138 |
+ |
|
139 |
+ |