1 /* Put log messages either to stdout or syslog.
3 * Assimilated from git.git::daemon.c on 2007-05-19. */
10 #include "libutil/Log.h"
12 namespace remote
{ namespace util
{
15 Log::open(const char *ident
, int level
, FILE *file
)
26 openlog(log_ident
, 0, LOG_DAEMON
);
31 Log::put(int priority
, const char *format
, va_list params
)
33 /* We should do a single write so that it is atomic and output
34 * of several processes do not get intermingled. */
44 /* sizeof(buf) should be big enough for "[pid] \n" */
45 buflen
= snprintf(buf
, sizeof(buf
), "[%ld] ", (long) getpid());
47 maxlen
= sizeof(buf
) - buflen
- 1; /* -1 for our own LF */
48 msglen
= vsnprintf(buf
+ buflen
, maxlen
, format
, params
);
51 syslog(priority
, "%s", buf
);
55 /* maxlen counted our own LF but also counts space given to
56 * vsnprintf for the terminating NUL. We want to make sure that
57 * we have space for our own LF and NUL after the "meat" of the
58 * message, so truncate it at maxlen - 1.
60 if (msglen
> maxlen
- 1)
63 msglen
= 0; /* Protect against weird return values. */
70 fprintf(log_file
, "%s - %s %s\n", strsep(×tr
, "\n"), log_ident
, buf
);
74 Log::error(const char *format
, ...)
78 va_start(params
, format
);
79 put(LOG_ERR
, format
, params
);
84 Log::warn(const char *format
, ...)
88 va_start(params
, format
);
89 put(LOG_WARNING
, format
, params
);
94 Log::info(const char *format
, ...)
98 va_start(params
, format
);
99 put(LOG_INFO
, format
, params
);
104 Log::debug(const char *format
, ...)
108 va_start(params
, format
);
109 put(LOG_DEBUG
, format
, params
);
113 const char *Log::log_ident
;