1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 15:27:45 +02:00
toxic/src/log.c

194 lines
5.1 KiB
C
Raw Normal View History

2014-03-02 00:06:35 +01:00
/* log.c
2014-02-26 07:51:06 +01:00
*
*
* Copyright (C) 2014 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toxic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
2014-02-26 07:51:06 +01:00
#include "configdir.h"
#include "toxic.h"
#include "windows.h"
2014-02-26 07:51:06 +01:00
#include "misc_tools.h"
2014-03-25 13:25:10 +01:00
#include "log.h"
2014-04-07 10:42:10 +02:00
#include "settings.h"
#include "line_info.h"
2014-04-07 10:42:10 +02:00
2014-07-21 01:12:13 +02:00
extern struct user_settings *user_settings_;
2014-02-26 07:51:06 +01:00
2014-03-04 01:21:52 +01:00
/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */
void init_logging_session(char *name, const char *key, struct chatlog *log)
2014-02-26 07:51:06 +01:00
{
if (!log->log_on)
2014-02-26 07:51:06 +01:00
return;
if (!valid_nick(name))
name = UNKNOWN_NAME;
2014-08-27 05:21:32 +02:00
const char *set_path = user_settings_->chatlogs_path;
2014-02-26 07:51:06 +01:00
char *user_config_dir = get_user_config_dir();
2014-08-27 05:21:32 +02:00
int path_len = strlen(set_path) + strlen(name) ? *set_path
: strlen(user_config_dir) + strlen(LOGDIR) + strlen(name);
2014-02-26 23:15:34 +01:00
/* use first 4 digits of key as log ident. If no key use a timestamp */
char ident[32];
2014-02-26 23:15:34 +01:00
if (key != NULL) {
path_len += (KEY_IDENT_DIGITS * 2 + 5);
sprintf(&ident[0], "%02X", key[0] & 0xff);
2014-03-21 09:03:07 +01:00
sprintf(&ident[2], "%02X", key[1] & 0xff);
ident[KEY_IDENT_DIGITS * 2 + 1] = '\0';
2014-02-26 23:15:34 +01:00
} else {
strftime(ident, sizeof(ident), "%Y-%m-%d[%H:%M:%S]", get_time());
2014-02-26 23:15:34 +01:00
path_len += strlen(ident) + 1;
}
2014-02-26 07:51:06 +01:00
if (path_len >= MAX_STR_SIZE) {
log->log_on = false;
2014-04-06 11:20:46 +02:00
free(user_config_dir);
2014-02-26 07:51:06 +01:00
return;
2014-02-26 23:15:34 +01:00
}
2014-02-26 07:51:06 +01:00
char log_path[MAX_STR_SIZE];
2014-08-27 05:21:32 +02:00
if (*set_path)
snprintf(log_path, sizeof(log_path), "%s%s-%s.log", set_path, name, ident);
else
snprintf(log_path, sizeof(log_path), "%s%s%s-%s.log", user_config_dir, LOGDIR, name, ident);
2014-02-26 07:51:06 +01:00
2014-04-06 11:20:46 +02:00
free(user_config_dir);
log->file = fopen(log_path, "a+");
snprintf(log->path, sizeof(log->path), "%s", log_path);
2014-02-26 07:51:06 +01:00
2014-03-04 01:21:52 +01:00
if (log->file == NULL) {
log->log_on = false;
2014-02-26 07:51:06 +01:00
return;
}
}
2014-09-11 07:36:33 +02:00
#define LOG_FLUSH_LIMIT 1 /* limits calls to fflush to a max of one per LOG_FLUSH_LIMIT seconds */
void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event)
2014-02-26 07:51:06 +01:00
{
if (!log->log_on)
2014-02-26 07:51:06 +01:00
return;
2014-03-04 01:21:52 +01:00
if (log->file == NULL) {
log->log_on = false;
2014-02-26 07:51:06 +01:00
return;
}
char name_frmt[TOXIC_MAX_NAME_LENGTH + 3];
2014-02-26 12:35:19 +01:00
if (event)
snprintf(name_frmt, sizeof(name_frmt), "* %s", name);
else
snprintf(name_frmt, sizeof(name_frmt), "%s:", name);
2014-07-21 01:12:13 +02:00
const char *t = user_settings_->time == TIME_12 ? "%Y/%m/%d [%I:%M:%S %p]" : "%Y/%m/%d [%H:%M:%S]";
char s[MAX_STR_SIZE];
2014-04-07 10:42:10 +02:00
strftime(s, MAX_STR_SIZE, t, get_time());
fprintf(log->file, "%s %s %s\n", s, name_frmt, msg);
2014-03-04 01:21:52 +01:00
2014-03-14 04:30:44 +01:00
uint64_t curtime = get_unix_time();
2014-03-04 01:21:52 +01:00
if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) {
2014-03-04 01:21:52 +01:00
fflush(log->file);
log->lastwrite = curtime;
}
}
void log_enable(char *name, const char *key, struct chatlog *log)
{
log->log_on = true;
2014-03-04 01:21:52 +01:00
if (log->file == NULL)
init_logging_session(name, key, log);
}
void log_disable(struct chatlog *log)
{
2014-03-04 01:21:52 +01:00
log->log_on = false;
if (log->file != NULL) {
fclose(log->file);
log->file = NULL;
}
2014-02-26 07:51:06 +01:00
}
/* Loads previous history from chat log */
void load_chat_history(ToxWindow *self, struct chatlog *log)
{
if (log->file == NULL)
return;
struct stat st;
if (stat(log->path, &st) == -1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Failed to stat log file");
return;
}
int sz = st.st_size;
if (sz <= 0)
return;
char *hstbuf = malloc(sz);
if (hstbuf == NULL)
exit_toxic_err("failed in print_prev_chat_history", FATALERR_MEMORY);
if (fread(hstbuf, sz, 1, log->file) != 1) {
free(hstbuf);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Failed to read log file");
return;
}
/* Number of history lines to load: must not be larger than MAX_LINE_INFO_QUEUE - 2 */
int L = MIN(MAX_LINE_INFO_QUEUE - 2, user_settings_->history_size);
int start, count = 0;
/* start at end and backtrace L lines or to the beginning of buffer */
for (start = sz - 1; start >= 0 && count < L; --start) {
if (hstbuf[start] == '\n')
++count;
}
const char *line = strtok(&hstbuf[start + 1], "\n");
if (line == NULL) {
free(hstbuf);
return;
}
while (line != NULL) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", line);
line = strtok(NULL, "\n");
}
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "");
free(hstbuf);
}