struct SilcLogStruct {
char *filename;
FILE *fp;
- uint32 maxsize;
+ SilcUInt32 maxsize;
char *typename;
SilcLogType type;
SilcLogCb cb;
};
typedef struct SilcLogStruct *SilcLog;
-/* These are the known logging channels */
+/* These are the known logging channels. We initialize this struct with most
+ * of the fields set to NULL, because we'll fill in those values at runtime. */
static struct SilcLogStruct silclogs[SILC_LOG_MAX] = {
{NULL, NULL, 0, "Info", SILC_LOG_INFO, NULL, NULL},
- {NULL, NULL, 0, "Error", SILC_LOG_ERROR, NULL, NULL},
{NULL, NULL, 0, "Warning", SILC_LOG_WARNING, NULL, NULL},
+ {NULL, NULL, 0, "Error", SILC_LOG_ERROR, NULL, NULL},
{NULL, NULL, 0, "Fatal", SILC_LOG_FATAL, NULL, NULL},
};
bool silc_debug = FALSE;
bool silc_debug_hexdump = FALSE;
-/* Flush delay */
+/* Flush delay (in seconds) */
long silc_log_flushdelay = 300;
/* Regular pattern matching expression for the debug output */
-static char *silc_log_debug_string = NULL;
+char *silc_log_debug_string = NULL;
-/* Debug callbacks. If set these are used instead of default ones. */
+/* Debug callbacks. If set, these are triggered for each specific output. */
static SilcLogDebugCb silc_log_debug_cb = NULL;
static void *silc_log_debug_context = NULL;
static SilcLogHexdumpCb silc_log_hexdump_cb = NULL;
static bool silc_log_scheduled = FALSE;
static bool silc_log_no_init = FALSE;
-/* This is only needed during starting up -- don't lose any logging */
+/* This is only needed during starting up -- don't lose any logging message */
static bool silc_log_starting = TRUE;
/* The type wrapper utility. Translates a SilcLogType id to the corresponding
if (!log->filename) return FALSE;
if (!(log->fp = fopen(log->filename, "a+"))) {
SILC_LOG_WARNING(("Couldn't reset logfile %s for type \"%s\": %s",
- log->filename, log->typename, strerror(errno)));
+ log->filename, log->typename, strerror(errno)));
return FALSE;
}
return TRUE;
/* Outputs the log message to the first available channel. Channels are
* ordered by importance (see SilcLogType documentation).
- * More importants channels can be printed on less important ones, but not
+ * More important channels can be printed on less important ones, but not
* vice-versa. */
void silc_log_output(SilcLogType type, char *string)
{
- char *typename;
+ char *typename = NULL;
FILE *fp;
SilcLog log;
if ((type > SILC_LOG_MAX) || !(log = silc_log_find_by_type(type)))
goto end;
+ /* Save the original typename, because even if we redirect the message
+ * to another channel we'll keep however the original channel name */
+ typename = log->typename;
+
/* If there is a custom callback set, use it and return. */
if (log->cb) {
if ((*log->cb)(type, string, log->context))
goto end;
}
- /* save the original typename, because if we redirect the channel we
- * keep however the original destination channel name */
- typename = log->typename;
-
if (!silc_log_scheduled) {
if (silc_log_no_init == FALSE) {
fprintf(stderr,
}
end:
+ /* If debugging, also output the logging message to the console */
+ if (typename && silc_debug) {
+ fprintf(stderr, "[Logging] [%s] %s\n", typename, string);
+ fflush(stderr);
+ }
silc_free(string);
}
}
/* Set and initialize the specified logging channel. See the API reference */
-bool silc_log_set_file(SilcLogType type, char *filename, uint32 maxsize,
+bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
SilcSchedule scheduler)
{
FILE *fp = NULL;
SILC_LOG_DEBUG(("Resetting all logs"));
SILC_FOREACH_LOG(u)
silc_log_reset(&silclogs[u]);
+ /* Immediately flush any possible warning message */
+ silc_log_flush_all();
}
/* Outputs the debug message to stderr. */
{
if (!silc_debug)
goto end;
+
if (silc_log_debug_string &&
- !silc_string_regex_match(silc_log_debug_string, file) &&
- !silc_string_regex_match(silc_log_debug_string, function))
+ !silc_string_regex_match(silc_log_debug_string, file) &&
+ !silc_string_regex_match(silc_log_debug_string, function))
goto end;
+
if (silc_log_debug_cb) {
if ((*silc_log_debug_cb)(file, function, line, string,
silc_log_debug_context))
goto end;
}
+
fprintf(stderr, "%s:%d: %s\n", function, line, string);
fflush(stderr);
-end:
+
+ end:
silc_free(string);
}
void silc_log_output_hexdump(char *file, char *function,
int line, void *data_in,
- uint32 len, char *string)
+ SilcUInt32 len, char *string)
{
int i, k;
int off, pos, count;
if (!silc_debug_hexdump)
goto end;
+
if (silc_log_debug_string &&
- !silc_string_regex_match(silc_log_debug_string, file) &&
- !silc_string_regex_match(silc_log_debug_string, function))
+ !silc_string_regex_match(silc_log_debug_string, file) &&
+ !silc_string_regex_match(silc_log_debug_string, function))
goto end;
+
if (silc_log_hexdump_cb) {
if ((*silc_log_hexdump_cb)(file, function, line, data_in, len, string,
silc_log_hexdump_context))
}
fprintf(stderr, "%s:%d: %s\n", function, line, string);
- silc_free(string);
k = 0;
pos = 0;
if (count < 16)
break;
}
- return;
-end:
+ end:
silc_free(string);
}
void silc_log_set_debug_string(const char *debug_string)
{
silc_free(silc_log_debug_string);
- if ((strchr(debug_string, '(') &&
- strchr(debug_string, ')')) ||
- strchr(debug_string, '$'))
+ if ((strchr(debug_string, '(') && strchr(debug_string, ')')) ||
+ strchr(debug_string, '$'))
silc_log_debug_string = strdup(debug_string);
else
silc_log_debug_string = silc_string_regexify(debug_string);