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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-03-11 00:34:18 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-02-26 07:51:06 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "configdir.h"
|
|
|
|
#include "toxic_windows.h"
|
|
|
|
#include "misc_tools.h"
|
|
|
|
|
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 */
|
2014-03-01 13:10:44 +01:00
|
|
|
void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log)
|
2014-02-26 07:51:06 +01:00
|
|
|
{
|
2014-03-01 13:10:44 +01:00
|
|
|
if (!log->log_on)
|
2014-02-26 07:51:06 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
char *user_config_dir = get_user_config_dir();
|
2014-02-26 23:15:34 +01:00
|
|
|
int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name);
|
|
|
|
|
|
|
|
/* use first 4 digits of key as log ident. If no key use a timestamp */
|
|
|
|
uint8_t ident[32];
|
|
|
|
|
|
|
|
if (key != NULL) {
|
|
|
|
path_len += (KEY_IDENT_DIGITS * 2 + 5);
|
|
|
|
|
|
|
|
sprintf(&ident[0], "%02X", key[0] & 0xff);
|
|
|
|
sprintf(&ident[2], "%02X", key[2] & 0xff);
|
|
|
|
ident[KEY_IDENT_DIGITS*2+1] = '\0';
|
|
|
|
} else {
|
2014-03-12 08:08:13 +01:00
|
|
|
uint8_t s[MAX_STR_SIZE];
|
|
|
|
strftime(s, MAX_STR_SIZE, "%Y-%m-%d[%H:%M:%S]", get_time());
|
|
|
|
snprintf(ident, sizeof(ident), "%s", s);
|
2014-02-26 23:15:34 +01:00
|
|
|
path_len += strlen(ident) + 1;
|
|
|
|
}
|
2014-02-26 07:51:06 +01:00
|
|
|
|
2014-02-26 23:15:34 +01:00
|
|
|
if (path_len > MAX_STR_SIZE) {
|
2014-03-01 13:10:44 +01:00
|
|
|
log->log_on = false;
|
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
|
|
|
|
2014-03-04 01:21:52 +01:00
|
|
|
uint8_t log_path[MAX_STR_SIZE];
|
|
|
|
|
|
|
|
snprintf(log_path, MAX_STR_SIZE, "%s%s%s-%s.log",
|
2014-02-26 09:51:26 +01:00
|
|
|
user_config_dir, CONFIGDIR, name, ident);
|
2014-02-26 07:51:06 +01:00
|
|
|
|
2014-03-04 01:21:52 +01:00
|
|
|
log->file = fopen(log_path, "a");
|
2014-02-26 07:51:06 +01:00
|
|
|
|
2014-03-04 01:21:52 +01:00
|
|
|
if (log->file == NULL) {
|
2014-03-01 13:10:44 +01:00
|
|
|
log->log_on = false;
|
2014-02-26 07:51:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-04 01:21:52 +01:00
|
|
|
fprintf(log->file, "\n*** NEW SESSION ***\n\n");
|
2014-02-26 07:51:06 +01:00
|
|
|
free(user_config_dir);
|
|
|
|
}
|
|
|
|
|
2014-03-04 01:21:52 +01:00
|
|
|
void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event)
|
2014-02-26 07:51:06 +01:00
|
|
|
{
|
2014-03-01 13:10:44 +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) {
|
2014-03-01 13:10:44 +01:00
|
|
|
log->log_on = false;
|
2014-02-26 07:51:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-26 12:35:19 +01:00
|
|
|
uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3];
|
|
|
|
|
|
|
|
if (event)
|
|
|
|
snprintf(name_frmt, sizeof(name_frmt), "* %s", name);
|
|
|
|
else
|
|
|
|
snprintf(name_frmt, sizeof(name_frmt), "%s:", name);
|
|
|
|
|
2014-03-12 08:08:13 +01:00
|
|
|
uint8_t s[MAX_STR_SIZE];
|
|
|
|
strftime(s, MAX_STR_SIZE, "%Y/%m/%d [%H:%M:%S]", get_time());
|
|
|
|
fprintf(log->file,"%s %s %s\n", s, name_frmt, msg);
|
2014-03-04 01:21:52 +01:00
|
|
|
|
|
|
|
uint64_t curtime = (uint64_t) time(NULL);
|
|
|
|
|
2014-03-05 09:31:12 +01:00
|
|
|
if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) {
|
2014-03-04 01:21:52 +01:00
|
|
|
fflush(log->file);
|
2014-03-05 09:31:12 +01:00
|
|
|
log->lastwrite = curtime;
|
|
|
|
}
|
2014-03-01 13:10:44 +01:00
|
|
|
}
|
|
|
|
|
2014-03-02 00:06:35 +01:00
|
|
|
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log)
|
2014-03-01 13:10:44 +01:00
|
|
|
{
|
|
|
|
log->log_on = true;
|
|
|
|
|
2014-03-04 01:21:52 +01:00
|
|
|
if (log->file == NULL)
|
2014-03-01 13:10:44 +01:00
|
|
|
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-03-01 13:10:44 +01:00
|
|
|
}
|
2014-02-26 07:51:06 +01:00
|
|
|
}
|