2 * UCW Library -- Logging
4 * (c) 1997--2009 Martin Mares <mj@ucw.cz>
5 * (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include "ucw/simple-lists.h"
24 static char log_progname[32];
27 void (*log_die_hook)(void);
30 static int ls_default_handler(struct log_stream* ls, const char *m, u32 cat UNUSED)
33 int r = write(ls->idata, m, len);
34 /* TODO: check the errors here? */
40 /* the default logger */
41 const struct log_stream ls_default_log={
42 .name = "fd2", .idata = 2, .pdata = NULL, .regnum = 0,
43 .handler = ls_default_handler,
44 .levels = LS_ALL_LEVELS,
45 .msgfmt = LSFMT_DEFAULT,
47 .substreams.head.next = (cnode *) &ls_default_log.substreams.head,
48 .substreams.head.prev = (cnode *) &ls_default_log.substreams.head,
51 /*** Registry of streams and their identifiers ***/
53 /* The growing array of pointers to log_streams. */
54 struct lsbuf_t log_streams;
56 /* the first never-used index in ls_streams.ptr */
57 int log_streams_after = 0;
59 /* get a stream by its LS_SET_STRNUM() */
60 /* returns NULL for free/invalid numbers */
61 /* defaults to ls_default_stream when stream number 0 closed */
62 struct log_stream *log_stream_by_flags(uns flags)
64 /* get the real number */
65 int n = LS_GET_STRNUM(flags);
66 if ((n<0) || (n>=log_streams_after) || (log_streams.ptr[n]->regnum==-1) )
69 return (struct log_stream *)&ls_default_log;
73 return log_streams.ptr[n];
78 void vmsg(unsigned int cat, const char *fmt, va_list args)
88 struct log_stream *ls=log_stream_by_flags(cat);
90 /* Check the stream existence */
93 msg((LS_INTERNAL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.",
95 ls=(struct log_stream *)&ls_default_log;
99 if (!(cat&LSFLAG_SIGHANDLER))
101 /* CAVEAT: These calls are not safe in signal handlers. */
102 gettimeofday(&tv, NULL);
103 if (localtime_r(&tv.tv_sec, &tm))
107 /* generate time strings */
110 strftime(stime, 24, "%Y-%m-%d %H:%M:%S", &tm);
111 snprintf(sutime, 12, ".%06d", (int)tv.tv_usec);
115 snprintf(stime, 24, "\?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
116 snprintf(sutime, 12, ".\?\?\?\?\?\?");
119 /* generate the message string */
120 va_copy(args2, args);
121 /* WARN: this may be C99 specefic */
122 len = vsnprintf(NULL, 0, fmt, args2);
124 buf = xmalloc(len+2);
125 vsnprintf(buf, len+1, fmt, args);
127 /* remove non-printable characters and newlines */
131 if (*p < 0x20 && *p != '\t')
136 /* pass the message to the log_stream */
137 if(ls_passmsg(0, ls, stime, sutime, buf, cat))
139 /* error (such as looping) occured */
140 ls_passmsg(0, (struct log_stream *)&ls_default_log, stime, sutime, buf, cat);
146 /* process a message (string) */
147 /* depth prevents undetected looping */
148 /* returns 1 in case of loop detection or other fatal error
150 int ls_passmsg(int depth, struct log_stream *ls, const char *stime, const char *sutime, const char *m, u32 cat)
154 /* Check recursion depth */
155 if( depth > LS_MAX_DEPTH )
157 ls_passmsg(0, (struct log_stream *)&ls_default_log, stime, sutime,
158 "Loop in the log_stream system detected.", L_ERROR | (cat&LS_INTERNAL_MASK) );
162 /* Filter by level and filter hook */
163 if(!( (1<<LS_GET_LEVEL(cat)) & ls->levels )) return 0;
165 if( ls->filter(ls, m, cat) != 0 ) return 0;
167 /* pass message to substreams */
168 CLIST_FOR_EACH(simp_node *, s, ls->substreams)
170 if (ls_passmsg(depth+1, (struct log_stream*)(s->p), stime, sutime, m, cat))
174 /* Prepare for handler */
177 int len = strlen(m) + strlen(stime) + strlen(sutime) + 32;
178 /* SHOULD be enough for all information, but beware */
179 if (log_title) len += strlen(log_title);
180 if (ls->name) len += strlen(ls->name);
181 char *buf=xmalloc(len);
184 /* Level (2 chars) */
185 if(ls->msgfmt & LSFMT_LEVEL)
187 *p++=LS_LEVEL_LETTER(LS_GET_LEVEL(cat));
191 /* Time (|stime| + |sutime| + 1 chars) */
192 if(ls->msgfmt & LSFMT_TIME)
194 char *q = (char *)stime;
198 if(ls->msgfmt & LSFMT_USEC)
207 /* process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
208 if((ls->msgfmt & LSFMT_TITLE) && log_title)
210 if(ls->msgfmt & LSFMT_PID)
211 p += sprintf(p, "[%s (%d)] ", log_title, getpid());
213 p += sprintf(p, "[%s] ", log_title);
217 if(ls->msgfmt & LSFMT_PID)
218 p += sprintf(p, "[%d] ", getpid());
221 /* log_stream name ( |ls->name| + 4 chars ) */
222 if(ls->msgfmt & LSFMT_LOGNAME)
225 p += sprintf(p, "<%s> ", ls->name);
227 p += sprintf(p, "<?> ");
230 /* finish the string and call the handler ( |m| + 1 chars ) */
238 ls->handler(ls, buf, cat);
246 msg(unsigned int cat, const char *fmt, ...)
251 vmsg(cat, fmt, args);
256 die(const char *fmt, ...)
261 vmsg(L_FATAL, fmt, args);
265 #ifdef DEBUG_DIE_BY_ABORT
273 assert_failed(const char *assertion, const char *file, int line)
275 msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
280 assert_failed_noinfo(void)
282 die("Internal error: Assertion failed.");
286 log_basename(const char *n)
297 log_init(const char *argv0)
301 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
302 log_progname[sizeof(log_progname)-1] = 0;
303 log_title = log_progname;
311 // ls_default_log.msgfmt |= LSFMT_USEC;
312 msg(L_INFO, "Brum!");