Add option to enable/disable chat logging

Also fix chat log file not always getting properly closed on errors
This commit is contained in:
UnknownShadow200 2020-05-27 00:08:38 +10:00
parent 33b2f15417
commit eab80ddee0
3 changed files with 29 additions and 19 deletions

View File

@ -50,6 +50,7 @@ static void AppendChatLogTime(void) {
static void ResetLogFile(void) { }
static void CloseLogFile(void) { }
void Chat_SetLogName(const String* name) { }
void Chat_DisableLogging(void) { }
static void OpenChatLog(struct DateTime* now) { }
static void AppendChatLog(const String* text) { }
#else
@ -61,12 +62,10 @@ static String logPath = String_FromArray(logPathBuffer);
static struct Stream logStream;
static struct DateTime lastLogDate;
/* Resets log name to empty and last log date to 0 */
/* Resets log name to empty and resets last log date */
static void ResetLogFile(void) {
logName.length = 0;
lastLogDate.day = 0;
lastLogDate.month = 0;
lastLogDate.year = 0;
logName.length = 0;
lastLogDate.year = -1234;
}
/* Closes handle to the chat log file */
@ -101,15 +100,17 @@ void Chat_SetLogName(const String* name) {
}
}
static void DisableChatLogging(void) {
Chat_Logging = false;
void Chat_DisableLogging(void) {
Chat_Logging = false;
lastLogDate.year = -5678;
Chat_AddRaw("&cDisabling chat logging");
CloseLogFile();
}
static void OpenChatLog(struct DateTime* now) {
cc_result res;
int i;
if (!Utils_EnsureDirectory("logs")) { DisableChatLogging(); return; }
if (!Utils_EnsureDirectory("logs")) { Chat_DisableLogging(); return; }
/* Ensure multiple instances do not end up overwriting each other's log entries. */
for (i = 0; i < 20; i++) {
@ -124,7 +125,7 @@ static void OpenChatLog(struct DateTime* now) {
res = Stream_AppendFile(&logStream, &logPath);
if (res && res != ReturnCode_FileShareViolation) {
DisableChatLogging();
Chat_DisableLogging();
Logger_Warn2(res, "appending to", &logPath);
return;
}
@ -133,8 +134,7 @@ static void OpenChatLog(struct DateTime* now) {
return;
}
logStream.Meta.File = 0;
DisableChatLogging();
Chat_DisableLogging();
Chat_Add1("&cFailed to open a chat log file after %i tries, giving up", &i);
}
@ -161,7 +161,7 @@ static void AppendChatLog(const String* text) {
res = Stream_WriteLine(&logStream, &str);
if (!res) return;
DisableChatLogging();
Chat_DisableLogging();
Logger_Warn2(res, "writing to", &logPath);
}
#endif

View File

@ -49,6 +49,8 @@ CC_API void Commands_Register(struct ChatCommand* cmd);
/* Sets the name of log file (no .txt, so e.g. just "singleplayer") */
/* NOTE: This can only be set once. */
void Chat_SetLogName(const String* name);
/* Disables chat logging and closes currently open chat log file. */
void Chat_DisableLogging(void);
/* Sends a chat message, raising ChatEvents.ChatSending event. */
/* NOTE: If logUsage is true, can press 'up' in chat input menu later to retype this. */
/* NOTE: /client is always interpreted as client-side commands. */

View File

@ -2593,9 +2593,6 @@ static void ChatOptionsScreen_SetScale(const String* v, float* target, const cha
Gui_RefreshChat();
}
static void ChatOptionsScreen_GetClickable(String* v) { Menu_GetBool(v, Gui_ClickableChat); }
static void ChatOptionsScreen_SetClickable(const String* v) { Gui_ClickableChat = Menu_SetBool(v, OPT_CLICKABLE_CHAT); }
static void ChatOptionsScreen_GetChatScale(String* v) { String_AppendFloat(v, Game_RawChatScale, 1); }
static void ChatOptionsScreen_SetChatScale(const String* v) { ChatOptionsScreen_SetScale(v, &Game_RawChatScale, OPT_CHAT_SCALE); }
@ -2606,23 +2603,34 @@ static void ChatOptionsScreen_SetChatlines(const String* v) {
Options_Set(OPT_CHATLINES, v);
}
static void ChatOptionsScreen_GetLogging(String* v) { Menu_GetBool(v, Chat_Logging); }
static void ChatOptionsScreen_SetLogging(const String* v) {
Chat_Logging = Menu_SetBool(v, OPT_CHAT_LOGGING);
if (!Chat_Logging) Chat_DisableLogging();
}
static void ChatOptionsScreen_GetClickable(String* v) { Menu_GetBool(v, Gui_ClickableChat); }
static void ChatOptionsScreen_SetClickable(const String* v) { Gui_ClickableChat = Menu_SetBool(v, OPT_CLICKABLE_CHAT); }
static void ChatOptionsScreen_InitWidgets(struct MenuOptionsScreen* s) {
static const struct MenuOptionDesc buttons[3] = {
static const struct MenuOptionDesc buttons[4] = {
{ -1, 0, "Chat scale", MenuOptionsScreen_Input,
ChatOptionsScreen_GetChatScale, ChatOptionsScreen_SetChatScale },
{ -1, 50, "Chat lines", MenuOptionsScreen_Input,
ChatOptionsScreen_GetChatlines, ChatOptionsScreen_SetChatlines },
{ 1, 0, "Log to disc", MenuOptionsScreen_Bool,
ChatOptionsScreen_GetLogging, ChatOptionsScreen_SetLogging },
{ 1, 50, "Clickable chat", MenuOptionsScreen_Bool,
ChatOptionsScreen_GetClickable, ChatOptionsScreen_SetClickable }
};
s->numWidgets = 3 + MENUOPTIONS_CORE_WIDGETS;
s->numWidgets = 4 + MENUOPTIONS_CORE_WIDGETS;
MenuOptionsScreen_InitButtons(s, buttons, Array_Elems(buttons), Menu_SwitchOptions);
}
void ChatOptionsScreen_Show(void) {
static struct MenuInputDesc descs[4];
static struct MenuInputDesc descs[5];
MenuInput_Float(descs[0], 0.25f, 4.00f, 1);
MenuInput_Int(descs[1], 0, 30, 10);
MenuOptionsScreen_Show(descs, NULL, 0, ChatOptionsScreen_InitWidgets);