void chat_log_chat(const gchar * const login, gchar *other, const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp) { struct dated_chat_log *dated_log = g_hash_table_lookup(logs, other); // no log for user if (dated_log == NULL) { dated_log = _create_log(other, login); g_hash_table_insert(logs, strdup(other), dated_log); // log exists but needs rolling } else if (_log_roll_needed(dated_log)) { dated_log = _create_log(other, login); g_hash_table_replace(logs, strdup(other), dated_log); } gchar *date_fmt = NULL; GDateTime *dt = NULL; if (tv_stamp == NULL) { dt = g_date_time_new_now_local(); } else { dt = g_date_time_new_from_timeval_utc(tv_stamp); } date_fmt = g_date_time_format(dt, "%H:%M:%S"); FILE *logp = fopen(dated_log->filename, "a"); g_chmod(dated_log->filename, S_IRUSR | S_IWUSR); if (logp != NULL) { if (direction == PROF_IN_LOG) { if (strncmp(msg, "/me ", 4) == 0) { fprintf(logp, "%s - *%s %s\n", date_fmt, other, msg + 4); } else { fprintf(logp, "%s - %s: %s\n", date_fmt, other, msg); } } else { if (strncmp(msg, "/me ", 4) == 0) { fprintf(logp, "%s - *me %s\n", date_fmt, msg + 4); } else { fprintf(logp, "%s - me: %s\n", date_fmt, msg); } } fflush(logp); int result = fclose(logp); if (result == EOF) { log_error("Error closing file %s, errno = %d", dated_log->filename, errno); } } g_free(date_fmt); g_date_time_unref(dt); }
void groupchat_log_chat(const gchar * const login, const gchar * const room, const gchar * const nick, const gchar * const msg) { gchar *room_copy = strdup(room); struct dated_chat_log *dated_log = g_hash_table_lookup(groupchat_logs, room_copy); // no log for room if (dated_log == NULL) { dated_log = _create_groupchat_log(room_copy, login); g_hash_table_insert(groupchat_logs, room_copy, dated_log); // log exists but needs rolling } else if (_log_roll_needed(dated_log)) { dated_log = _create_groupchat_log(room_copy, login); g_hash_table_replace(logs, room_copy, dated_log); } GDateTime *dt = g_date_time_new_now_local(); gchar *date_fmt = g_date_time_format(dt, "%H:%M:%S"); FILE *logp = fopen(dated_log->filename, "a"); g_chmod(dated_log->filename, S_IRUSR | S_IWUSR); if (logp) { if (strncmp(msg, "/me ", 4) == 0) { fprintf(logp, "%s - *%s %s\n", date_fmt, nick, msg + 4); } else { fprintf(logp, "%s - %s: %s\n", date_fmt, nick, msg); } fflush(logp); int result = fclose(logp); if (result == EOF) { log_error("Error closing file %s, errno = %d", dated_log->filename, errno); } } g_free(date_fmt); g_date_time_unref(dt); }