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
, const 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((FILE *) Log::file
, "%s - %s %s\n", strsep(×tr
, "\n"), Log::ident
, buf
);
74 Log::fatal(const char *format
, ...)
78 va_start(params
, format
);
79 put(Log::FATAL
, format
, params
);
84 Log::error(const char *format
, ...)
88 va_start(params
, format
);
89 put(Log::ERROR
, format
, params
);
94 Log::warn(const char *format
, ...)
98 va_start(params
, format
);
99 put(Log::WARN
, format
, params
);
104 Log::info(const char *format
, ...)
108 va_start(params
, format
);
109 put(Log::INFO
, format
, params
);
114 Log::debug(const char *format
, ...)
118 va_start(params
, format
);
119 put(Log::DEBUG
, format
, params
);
123 const char *Log::ident
;
124 const FILE *Log::file
;
126 const FILE *Log::SYSLOG
= NULL
;