+void yaz_log_init_max_size(int mx)
+{
+ if (mx > 0)
+ l_max_size = mx;
+ else
+ l_max_size = 0;
+}
+
+void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
+{
+ hook_func = func;
+ hook_info = info;
+}
+
+void log_event_start(void (*func)(int, const char *, void *), void *info)
+{
+ start_hook_func = func;
+ start_hook_info = info;
+}
+
+void log_event_end(void (*func)(int, const char *, void *), void *info)
+{
+ end_hook_func = func;
+ end_hook_info = info;
+}
+
+static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
+{
+ char new_filename[512];
+ static char cur_filename[512] = "";
+
+ if (yaz_file_type != use_file)
+ return;
+
+ if (l_fname && *l_fname)
+ {
+ strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
+ if (strcmp(new_filename, cur_filename))
+ {
+ strcpy(cur_filename, new_filename);
+ force = 1;
+ }
+ }
+
+ if (l_max_size > 0 && yaz_global_log_file)
+ {
+ long flen = ftell(yaz_global_log_file);
+ if (flen > l_max_size)
+ {
+ rotate_log(cur_filename);
+ force = 1;
+ }
+ }
+ if (force && *cur_filename)
+ {
+ FILE *new_file;
+#ifdef WIN32
+ yaz_log_close();
+#endif
+ new_file = fopen(cur_filename, filemode);
+ if (new_file)
+ {
+ yaz_log_close();
+ yaz_global_log_file = new_file;
+ if (l_level & YLOG_FLUSH)
+ setvbuf(yaz_global_log_file, 0, _IONBF, 0);
+ }
+ else
+ {
+ /* disable log rotate */
+ l_max_size = 0;
+ }
+ }
+}
+
+static void yaz_log_do_reopen(const char *filemode)
+{
+ time_t cur_time = time(0);
+#if HAVE_LOCALTIME_R
+ struct tm tm0, *tm = &tm0;
+#else
+ struct tm *tm;
+#endif
+
+ nmem_mutex_enter(log_mutex);
+#if HAVE_LOCALTIME_R
+ localtime_r(&cur_time, tm);
+#else
+ tm = localtime(&cur_time);
+#endif
+ yaz_log_open_check(tm, 1, filemode);
+ nmem_mutex_leave(log_mutex);
+}