From 9491068bfef5b188c223896acb137df17b01374f Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Mon, 3 Jul 2017 11:43:51 -0400 Subject: [PATCH] finished setting up unix logging --- 4ed.cpp | 16 ++++++-- 4ed.h | 10 ++++- platform_linux/linux_4ed.cpp | 13 +++---- platform_unix/unix_4ed_functions.cpp | 58 ++++++++++++++++++---------- 4 files changed, 64 insertions(+), 33 deletions(-) diff --git a/4ed.cpp b/4ed.cpp index 3c9cd4db..628404ec 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -648,7 +648,8 @@ enum Command_Line_Action{ CLAct_WindowStreamMode, CLAct_FontSize, CLAct_FontUseHinting, - CLAct_Log, + CLAct_LogStdout, + CLAct_LogFile, CLAct_Count }; @@ -704,7 +705,8 @@ init_command_line_settings(App_Settings *settings, Plat_Settings *plat_settings, case 'f': action = CLAct_FontSize; break; case 'h': action = CLAct_FontUseHinting; --i; break; - case 'L': action = CLAct_Log; --i; break; + case 'l': action = CLAct_LogStdout; --i; break; + case 'L': action = CLAct_LogFile; --i; break; } } else if (arg[0] != 0){ @@ -807,9 +809,15 @@ init_command_line_settings(App_Settings *settings, Plat_Settings *plat_settings, action = CLAct_Nothing; }break; - case CLAct_Log: + case CLAct_LogStdout: { - plat_settings->use_log = true; + plat_settings->use_log = LogTo_Stdout; + action = CLAct_Nothing; + }break; + + case CLAct_LogFile: + { + plat_settings->use_log = LogTo_LogFile; action = CLAct_Nothing; }break; } diff --git a/4ed.h b/4ed.h index 8f325746..edf74c84 100644 --- a/4ed.h +++ b/4ed.h @@ -51,13 +51,21 @@ struct Command_Line_Parameters{ int32_t argc; }; +typedef u8 Log_To_Type; +enum{ + LogTo_Nothing, + LogTo_Stdout, + LogTo_LogFile, + LogTo_COUNT +}; + struct Plat_Settings{ char *custom_dll; b8 custom_dll_is_strict; b8 fullscreen_window; b8 stream_mode; - b8 use_log; + u8 use_log; i32 window_w, window_h; i32 window_x, window_y; diff --git a/platform_linux/linux_4ed.cpp b/platform_linux/linux_4ed.cpp index 8aa2fded..4e6af221 100644 --- a/platform_linux/linux_4ed.cpp +++ b/platform_linux/linux_4ed.cpp @@ -86,13 +86,9 @@ #define FPS 60L #define frame_useconds (1000000UL / FPS) -#if FRED_INTERNAL #define LINUX_FN_DEBUG(fmt, ...) do { \ - fprintf(stderr, "%s: " fmt "\n", __func__, ##__VA_ARGS__); \ + LOGF("%s: " fmt "\n", __func__, ##__VA_ARGS__); \ } while(0) -#else -#define LINUX_FN_DEBUG(fmt, ...) -#endif #define InterlockedCompareExchange(dest, ex, comp) \ __sync_val_compare_and_swap((dest), (comp), (ex)) @@ -204,8 +200,6 @@ struct Linux_Vars{ Linux_Coroutine coroutine_data[18]; Linux_Coroutine *coroutine_free; - - u32 log_position; }; // @@ -922,6 +916,7 @@ LinuxLoadSystemCode(){ linuxvars.system.release_lock = system_release_lock; // debug + linuxvars.system.log = system_log; #if FRED_INTERNAL linuxvars.system.internal_get_thread_states = internal_get_thread_states; #endif @@ -2353,9 +2348,11 @@ main(int argc, char **argv){ } if (output_size != 0){ LinuxFatalErrorMsg("Error reading command-line arguments."); - return 1; + return(1); } + unixvars.use_log = linuxvars.settings.use_log; + sysshared_filter_real_files(files, file_count); // diff --git a/platform_unix/unix_4ed_functions.cpp b/platform_unix/unix_4ed_functions.cpp index c33b1b02..994d14af 100644 --- a/platform_unix/unix_4ed_functions.cpp +++ b/platform_unix/unix_4ed_functions.cpp @@ -28,7 +28,8 @@ #endif struct Unix_Vars{ - b32 do_logging; + u32 use_log; + b32 did_first_log; }; static Unix_Vars unixvars; @@ -58,8 +59,22 @@ Sys_Get_4ed_Path_Sig(system_get_4ed_path){ internal Sys_Log_Sig(system_log){ - if (unixvars.do_logging){ - i32 fd = open("4coder_log.txt", O_WRONLY | O_CREAT, 00640); + if (unixvars.use_log == LogTo_LogFile){ + char file_path_space[1024]; + String file_path = make_fixed_width_string(file_path_space); + file_path.size = system_get_4ed_path(file_path.str, file_path.memory_size); + append_sc(&file_path, "4coder_log.txt"); + terminate_with_null(&file_path); + + i32 fd = -1; + if (unixvars.did_first_log){ + fd = open(file_path.str, O_WRONLY | O_CREAT | O_APPEND, 00640); + } + else{ + fd = open(file_path.str, O_WRONLY | O_CREAT | O_TRUNC, 00640); + unixvars.did_first_log = true; + } + if (fd >= 0){ do{ ssize_t written = write(fd, message, length); @@ -71,6 +86,9 @@ Sys_Log_Sig(system_log){ close(fd); } } + else if (unixvars.use_log == LogTo_Stdout){ + fwrite(message, 1, length, stdout); + } } // @@ -80,7 +98,7 @@ Sys_Log_Sig(system_log){ internal Sys_File_Can_Be_Made_Sig(system_file_can_be_made){ b32 result = access((char*)filename, W_OK) == 0; - LOGF("%s = %d", filename, result); + LOGF("%s = %d\n", filename, result); return(result); } @@ -94,7 +112,7 @@ Sys_Memory_Allocate_Sig(system_memory_allocate){ // We will count on the user to keep track of size themselves. void *result = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if(result == MAP_FAILED){ - LOG("mmap failed\n"); + LOG("error: mmap failed\n"); result = NULL; } return(result); @@ -129,7 +147,7 @@ Sys_Memory_Set_Protection_Sig(system_memory_set_protection){ if(mprotect(ptr, size, protect) == -1){ result = 0; - LOG("mprotect"); + LOG("error: mprotect\n"); } return(result); @@ -156,7 +174,7 @@ Sys_Set_File_List_Sig(system_set_file_list){ return; } - LOGF("%s", directory); + LOGF("set_file_list: %s\n", directory); DIR *d = opendir(directory); if (d != 0){ @@ -288,7 +306,7 @@ Sys_Get_Canonical_Sig(system_get_canonical){ #if FRED_INTERNAL if(len != (write_p - path) || memcmp(filename, path, len) != 0){ - LOGF("[%.*s] -> [%.*s]", len, filename, (int)(write_p - path), path); + LOGF("[%.*s] -> [%.*s]\n", len, filename, (int)(write_p - path), path); } #endif @@ -299,14 +317,14 @@ Sys_Get_Canonical_Sig(system_get_canonical){ internal Sys_Load_Handle_Sig(system_load_handle){ - b32 result = 0; + b32 result = false; - int fd = open(filename, O_RDONLY); + i32 fd = open(filename, O_RDONLY); if(fd == -1){ - LOG("open"); + LOG("error: open\n"); } else { *(int*)handle_out = fd; - result = 1; + result = true; } return result; @@ -320,7 +338,7 @@ Sys_Load_Size_Sig(system_load_size){ struct stat st; if(fstat(fd, &st) == -1){ - LOG("fstat"); + LOG("error: fstat\n"); } else{ result = st.st_size; @@ -337,7 +355,7 @@ Sys_Load_File_Sig(system_load_file){ ssize_t n = read(fd, buffer, size); if(n == -1){ if(errno != EINTR){ - LOG("read"); + LOG("error: read\n"); break; } } @@ -356,7 +374,7 @@ Sys_Load_Close_Sig(system_load_close){ int fd = *(int*)&handle; if(close(fd) == -1){ - LOG("close"); + LOG("error: close\n"); result = 0; } @@ -366,16 +384,16 @@ Sys_Load_Close_Sig(system_load_close){ internal Sys_Save_File_Sig(system_save_file){ int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 00640); - LOGF("%s %d", filename, size); + LOGF("%s %d\n", filename, size); if(fd < 0){ - LOGF("system_save_file: open '%s': %s\n", filename, strerror(errno)); + LOGF("error: open '%s': %s\n", filename, strerror(errno)); } else{ do { ssize_t written = write(fd, buffer, size); if(written == -1){ if(errno != EINTR){ - LOG("system_save_file: write"); + LOG("error: write\n"); break; } } else { @@ -408,7 +426,7 @@ Sys_File_Exists_Sig(system_file_exists){ result = stat(buff, &st) == 0 && S_ISREG(st.st_mode); } - LOGF("%s: %d", buff, result); + LOGF("%s: %d\n", buff, result); return(result); } @@ -445,7 +463,7 @@ Sys_Directory_CD_Sig(system_directory_cd){ } *len = directory.size; - LOGF("%.*s: %d", directory.size, directory.str, result); + LOGF("%.*s: %d\n", directory.size, directory.str, result); return(result); }