mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
refactored logging
Use ftell instead of stat syscall to increase portability.
This commit is contained in:
parent
0346fda0b3
commit
28ec3334cf
27
src/log.c
27
src/log.c
@ -25,8 +25,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#include "glib.h"
|
#include "glib.h"
|
||||||
|
|
||||||
@ -37,11 +35,6 @@
|
|||||||
|
|
||||||
#define PROF "prof"
|
#define PROF "prof"
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
// replace 'struct stat' and 'stat()' for windows
|
|
||||||
#define stat _stat
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static FILE *logp;
|
static FILE *logp;
|
||||||
|
|
||||||
static GTimeZone *tz;
|
static GTimeZone *tz;
|
||||||
@ -125,7 +118,7 @@ log_init(log_level_t filter)
|
|||||||
tz = g_time_zone_new_local();
|
tz = g_time_zone_new_local();
|
||||||
gchar *log_file = _get_log_file();
|
gchar *log_file = _get_log_file();
|
||||||
logp = fopen(log_file, "a");
|
logp = fopen(log_file, "a");
|
||||||
g_free(log_file);
|
free(log_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
log_level_t
|
log_level_t
|
||||||
@ -145,9 +138,7 @@ void
|
|||||||
log_msg(log_level_t level, const char * const area, const char * const msg)
|
log_msg(log_level_t level, const char * const area, const char * const msg)
|
||||||
{
|
{
|
||||||
if (level >= level_filter) {
|
if (level >= level_filter) {
|
||||||
struct stat st;
|
long result;
|
||||||
int result;
|
|
||||||
gchar *log_file = _get_log_file();
|
|
||||||
dt = g_date_time_new_now(tz);
|
dt = g_date_time_new_now(tz);
|
||||||
|
|
||||||
gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S");
|
gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S");
|
||||||
@ -157,12 +148,10 @@ log_msg(log_level_t level, const char * const area, const char * const msg)
|
|||||||
fflush(logp);
|
fflush(logp);
|
||||||
g_free(date_fmt);
|
g_free(date_fmt);
|
||||||
|
|
||||||
result = stat(log_file, &st);
|
result = ftell(logp);
|
||||||
if (result == 0 && st.st_size >= prefs_get_max_log_size()) {
|
if (result != -1 && result >= prefs_get_max_log_size()) {
|
||||||
_rotate_log_file();
|
_rotate_log_file();
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(log_file);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,7 +189,7 @@ _rotate_log_file(void)
|
|||||||
log_init(log_get_filter());
|
log_init(log_get_filter());
|
||||||
|
|
||||||
free(log_file_new);
|
free(log_file_new);
|
||||||
g_free(log_file);
|
free(log_file);
|
||||||
log_info("Log has been rotated");
|
log_info("Log has been rotated");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -448,7 +437,7 @@ _get_log_filename(const char * const other, const char * const login,
|
|||||||
{
|
{
|
||||||
gchar *chatlogs_dir = _get_chatlog_dir();
|
gchar *chatlogs_dir = _get_chatlog_dir();
|
||||||
GString *log_file = g_string_new(chatlogs_dir);
|
GString *log_file = g_string_new(chatlogs_dir);
|
||||||
g_free(chatlogs_dir);
|
free(chatlogs_dir);
|
||||||
|
|
||||||
gchar *login_dir = str_replace(login, "@", "_at_");
|
gchar *login_dir = str_replace(login, "@", "_at_");
|
||||||
g_string_append_printf(log_file, "/%s", login_dir);
|
g_string_append_printf(log_file, "/%s", login_dir);
|
||||||
@ -516,7 +505,7 @@ _get_chatlog_dir(void)
|
|||||||
GString *chatlogs_dir = g_string_new(xdg_data);
|
GString *chatlogs_dir = g_string_new(xdg_data);
|
||||||
g_string_append(chatlogs_dir, "/profanity/chatlogs");
|
g_string_append(chatlogs_dir, "/profanity/chatlogs");
|
||||||
gchar *result = strdup(chatlogs_dir->str);
|
gchar *result = strdup(chatlogs_dir->str);
|
||||||
g_free(xdg_data);
|
free(xdg_data);
|
||||||
g_string_free(chatlogs_dir, TRUE);
|
g_string_free(chatlogs_dir, TRUE);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -529,7 +518,7 @@ _get_log_file(void)
|
|||||||
GString *logfile = g_string_new(xdg_data);
|
GString *logfile = g_string_new(xdg_data);
|
||||||
g_string_append(logfile, "/profanity/logs/profanity.log");
|
g_string_append(logfile, "/profanity/logs/profanity.log");
|
||||||
gchar *result = strdup(logfile->str);
|
gchar *result = strdup(logfile->str);
|
||||||
g_free(xdg_data);
|
free(xdg_data);
|
||||||
g_string_free(logfile, TRUE);
|
g_string_free(logfile, TRUE);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
Loading…
Reference in New Issue
Block a user