- time_t tim;
- struct tm *tm;
- char buf[32];
-
- tim = time(NULL);
- tm = localtime(&tim);
- strftime(buf, sizeof(buf), "%d-%m-%y %H-%M-%S", tm);
- fprintf(stderr, "%s %s [%d] <%d> ", buf, progname, pid, level);
- vfprintf(stderr, msg, args);
- fputc('\n', stderr);
- fflush(stderr);
+ time_t tim = time(NULL);
+ struct tm *tm = localtime(&tim);
+ byte *buf, *p;
+ int buflen = 256;
+ int l, l0, r;
+
+ log_switch(tm);
+ while (1)
+ {
+ p = buf = alloca(buflen);
+ *p++ = cat;
+ p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
+ if (log_progname)
+ {
+ if (log_pid)
+ p += sprintf(p, "[%s (%d)] ", log_progname, log_pid);
+ else
+ p += sprintf(p, "[%s] ", log_progname);
+ }
+ else
+ {
+ if (log_pid)
+ p += sprintf(p, "[%d] ", log_pid);
+ }
+ l0 = p - buf + 1;
+ r = buflen - l0;
+ l = vsnprintf(p, r, msg, args);
+ if (l < 0)
+ l = r;
+ else if (l < r)
+ {
+ while (*p)
+ {
+ if (*p < 0x20 && *p != '\t')
+ *p = 0x7f;
+ p++;
+ }
+ *p = '\n';
+ write(2, buf, l + l0);
+ return;
+ }
+ buflen = l + l0 + 1;
+ }