finished setting up unix logging

This commit is contained in:
Allen Webster 2017-07-03 11:43:51 -04:00
parent d2fa7aa868
commit 9491068bfe
4 changed files with 64 additions and 33 deletions

16
4ed.cpp
View File

@ -648,7 +648,8 @@ enum Command_Line_Action{
CLAct_WindowStreamMode, CLAct_WindowStreamMode,
CLAct_FontSize, CLAct_FontSize,
CLAct_FontUseHinting, CLAct_FontUseHinting,
CLAct_Log, CLAct_LogStdout,
CLAct_LogFile,
CLAct_Count CLAct_Count
}; };
@ -704,7 +705,8 @@ init_command_line_settings(App_Settings *settings, Plat_Settings *plat_settings,
case 'f': action = CLAct_FontSize; break; case 'f': action = CLAct_FontSize; break;
case 'h': action = CLAct_FontUseHinting; --i; 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){ else if (arg[0] != 0){
@ -807,9 +809,15 @@ init_command_line_settings(App_Settings *settings, Plat_Settings *plat_settings,
action = CLAct_Nothing; action = CLAct_Nothing;
}break; }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; action = CLAct_Nothing;
}break; }break;
} }

10
4ed.h
View File

@ -51,13 +51,21 @@ struct Command_Line_Parameters{
int32_t argc; int32_t argc;
}; };
typedef u8 Log_To_Type;
enum{
LogTo_Nothing,
LogTo_Stdout,
LogTo_LogFile,
LogTo_COUNT
};
struct Plat_Settings{ struct Plat_Settings{
char *custom_dll; char *custom_dll;
b8 custom_dll_is_strict; b8 custom_dll_is_strict;
b8 fullscreen_window; b8 fullscreen_window;
b8 stream_mode; b8 stream_mode;
b8 use_log; u8 use_log;
i32 window_w, window_h; i32 window_w, window_h;
i32 window_x, window_y; i32 window_x, window_y;

View File

@ -86,13 +86,9 @@
#define FPS 60L #define FPS 60L
#define frame_useconds (1000000UL / FPS) #define frame_useconds (1000000UL / FPS)
#if FRED_INTERNAL
#define LINUX_FN_DEBUG(fmt, ...) do { \ #define LINUX_FN_DEBUG(fmt, ...) do { \
fprintf(stderr, "%s: " fmt "\n", __func__, ##__VA_ARGS__); \ LOGF("%s: " fmt "\n", __func__, ##__VA_ARGS__); \
} while(0) } while(0)
#else
#define LINUX_FN_DEBUG(fmt, ...)
#endif
#define InterlockedCompareExchange(dest, ex, comp) \ #define InterlockedCompareExchange(dest, ex, comp) \
__sync_val_compare_and_swap((dest), (comp), (ex)) __sync_val_compare_and_swap((dest), (comp), (ex))
@ -204,8 +200,6 @@ struct Linux_Vars{
Linux_Coroutine coroutine_data[18]; Linux_Coroutine coroutine_data[18];
Linux_Coroutine *coroutine_free; Linux_Coroutine *coroutine_free;
u32 log_position;
}; };
// //
@ -922,6 +916,7 @@ LinuxLoadSystemCode(){
linuxvars.system.release_lock = system_release_lock; linuxvars.system.release_lock = system_release_lock;
// debug // debug
linuxvars.system.log = system_log;
#if FRED_INTERNAL #if FRED_INTERNAL
linuxvars.system.internal_get_thread_states = internal_get_thread_states; linuxvars.system.internal_get_thread_states = internal_get_thread_states;
#endif #endif
@ -2353,9 +2348,11 @@ main(int argc, char **argv){
} }
if (output_size != 0){ if (output_size != 0){
LinuxFatalErrorMsg("Error reading command-line arguments."); LinuxFatalErrorMsg("Error reading command-line arguments.");
return 1; return(1);
} }
unixvars.use_log = linuxvars.settings.use_log;
sysshared_filter_real_files(files, file_count); sysshared_filter_real_files(files, file_count);
// //

View File

@ -28,7 +28,8 @@
#endif #endif
struct Unix_Vars{ struct Unix_Vars{
b32 do_logging; u32 use_log;
b32 did_first_log;
}; };
static Unix_Vars unixvars; static Unix_Vars unixvars;
@ -58,8 +59,22 @@ Sys_Get_4ed_Path_Sig(system_get_4ed_path){
internal internal
Sys_Log_Sig(system_log){ Sys_Log_Sig(system_log){
if (unixvars.do_logging){ if (unixvars.use_log == LogTo_LogFile){
i32 fd = open("4coder_log.txt", O_WRONLY | O_CREAT, 00640); 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){ if (fd >= 0){
do{ do{
ssize_t written = write(fd, message, length); ssize_t written = write(fd, message, length);
@ -71,6 +86,9 @@ Sys_Log_Sig(system_log){
close(fd); close(fd);
} }
} }
else if (unixvars.use_log == LogTo_Stdout){
fwrite(message, 1, length, stdout);
}
} }
// //
@ -80,7 +98,7 @@ Sys_Log_Sig(system_log){
internal internal
Sys_File_Can_Be_Made_Sig(system_file_can_be_made){ Sys_File_Can_Be_Made_Sig(system_file_can_be_made){
b32 result = access((char*)filename, W_OK) == 0; b32 result = access((char*)filename, W_OK) == 0;
LOGF("%s = %d", filename, result); LOGF("%s = %d\n", filename, result);
return(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. // 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); void *result = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(result == MAP_FAILED){ if(result == MAP_FAILED){
LOG("mmap failed\n"); LOG("error: mmap failed\n");
result = NULL; result = NULL;
} }
return(result); return(result);
@ -129,7 +147,7 @@ Sys_Memory_Set_Protection_Sig(system_memory_set_protection){
if(mprotect(ptr, size, protect) == -1){ if(mprotect(ptr, size, protect) == -1){
result = 0; result = 0;
LOG("mprotect"); LOG("error: mprotect\n");
} }
return(result); return(result);
@ -156,7 +174,7 @@ Sys_Set_File_List_Sig(system_set_file_list){
return; return;
} }
LOGF("%s", directory); LOGF("set_file_list: %s\n", directory);
DIR *d = opendir(directory); DIR *d = opendir(directory);
if (d != 0){ if (d != 0){
@ -288,7 +306,7 @@ Sys_Get_Canonical_Sig(system_get_canonical){
#if FRED_INTERNAL #if FRED_INTERNAL
if(len != (write_p - path) || memcmp(filename, path, len) != 0){ 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 #endif
@ -299,14 +317,14 @@ Sys_Get_Canonical_Sig(system_get_canonical){
internal internal
Sys_Load_Handle_Sig(system_load_handle){ 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){ if(fd == -1){
LOG("open"); LOG("error: open\n");
} else { } else {
*(int*)handle_out = fd; *(int*)handle_out = fd;
result = 1; result = true;
} }
return result; return result;
@ -320,7 +338,7 @@ Sys_Load_Size_Sig(system_load_size){
struct stat st; struct stat st;
if(fstat(fd, &st) == -1){ if(fstat(fd, &st) == -1){
LOG("fstat"); LOG("error: fstat\n");
} }
else{ else{
result = st.st_size; result = st.st_size;
@ -337,7 +355,7 @@ Sys_Load_File_Sig(system_load_file){
ssize_t n = read(fd, buffer, size); ssize_t n = read(fd, buffer, size);
if(n == -1){ if(n == -1){
if(errno != EINTR){ if(errno != EINTR){
LOG("read"); LOG("error: read\n");
break; break;
} }
} }
@ -356,7 +374,7 @@ Sys_Load_Close_Sig(system_load_close){
int fd = *(int*)&handle; int fd = *(int*)&handle;
if(close(fd) == -1){ if(close(fd) == -1){
LOG("close"); LOG("error: close\n");
result = 0; result = 0;
} }
@ -366,16 +384,16 @@ Sys_Load_Close_Sig(system_load_close){
internal internal
Sys_Save_File_Sig(system_save_file){ Sys_Save_File_Sig(system_save_file){
int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 00640); 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){ if(fd < 0){
LOGF("system_save_file: open '%s': %s\n", filename, strerror(errno)); LOGF("error: open '%s': %s\n", filename, strerror(errno));
} }
else{ else{
do { do {
ssize_t written = write(fd, buffer, size); ssize_t written = write(fd, buffer, size);
if(written == -1){ if(written == -1){
if(errno != EINTR){ if(errno != EINTR){
LOG("system_save_file: write"); LOG("error: write\n");
break; break;
} }
} else { } else {
@ -408,7 +426,7 @@ Sys_File_Exists_Sig(system_file_exists){
result = stat(buff, &st) == 0 && S_ISREG(st.st_mode); result = stat(buff, &st) == 0 && S_ISREG(st.st_mode);
} }
LOGF("%s: %d", buff, result); LOGF("%s: %d\n", buff, result);
return(result); return(result);
} }
@ -445,7 +463,7 @@ Sys_Directory_CD_Sig(system_directory_cd){
} }
*len = directory.size; *len = directory.size;
LOGF("%.*s: %d", directory.size, directory.str, result); LOGF("%.*s: %d\n", directory.size, directory.str, result);
return(result); return(result);
} }