commit 24df7e4f19e86902d654c4fd0ef5687be9d2ace2
parent 11d268ba86008c303feb44aca1299bba0ca29a9e
Author: Michael Savage <mikejsavage@gmail.com>
Date: Sun, 29 Oct 2017 22:58:37 +0200
Put logs in $HOME or AppData
Diffstat:
5 files changed, 62 insertions(+), 3 deletions(-)
diff --git a/log.cc b/log.cc
@@ -4,12 +4,15 @@
#include "intrinsics.h"
#include "log.h"
#include "str.h"
+#include "dynstr.h"
+#include "patterns.h"
#include "platform_atomic.h"
#include "platform_thread.h"
#include "platform_mutex.h"
+#include "platform_home.h"
static FILE * streams[ LOGLEVEL_COUNT + 1 ];
-static str< 256 > logs_dir;
+static DynamicString logs_dir;
static const char * file_names[ LOGLEVEL_COUNT + 1 ] = {
"fatals",
@@ -30,14 +33,33 @@ static atomic_u32 num_threads;
static Mutex mutex;
+static void recursive_mkdir( const char * path ) {
+ DynamicString cur;
+ if( path[ 0 ] == '/' ) {
+ cur += '/';
+ }
+ // TODO: test this with drives on windows!
+ // TODO: error checking
+ for( array< array< const char > > matches : gmatch( path, "([^/\\]+)" ) ) {
+ cur.append( matches[ 0 ].ptr(), matches[ 0 ].n );
+ cur += '/';
+ mkdir( cur.c_str(), 0755 );
+ }
+}
+
void logger_init() {
mutex_init( &mutex );
+#if RELEASE_BUILD
+ get_config_directory( logs_dir );
+ recursive_mkdir( logs_dir.c_str() );
+#else
mkdir( "logs", 0755 );
u64 now = checked_cast< u64 >( time( NULL ) );
logs_dir.sprintf( "logs/{}", now );
mkdir( logs_dir.c_str(), 0755 );
+#endif
for( u32 i = 0; i <= LOGLEVEL_COUNT; i++ ) {
const str< 256 > path( "{}/{}.log", logs_dir, file_names[ i ] );
diff --git a/make.lua b/make.lua
@@ -1,6 +1,6 @@
require( "scripts.gen_makefile" )
-local common_objs = { "memory_arena", "log", "ggformat", "strlcpy", "strlcat", "strtonum", "profiler", "stats", "rng/well512", "breakbools" }
+local common_objs = { "memory_arena", "log", "ggformat", "patterns", "strlcpy", "strlcat", "strtonum", "profiler", "stats", "rng/well512", "breakbools" }
local game_objs = { "work_queue", "renderer", "shaders", "gl", "glad", "immediate", "text_renderer", "obj", "blue_noise", common_objs }
local game_libs = { "glfw", "lodepng", "stb_truetype", "tinyobjloader" }
@@ -22,7 +22,7 @@ bin( "medfall", { "main", "hm", "heightmap", "terrain_manager", "btt", "gpubtt",
msvc_bin_ldflags( "medfall", "opengl32.lib gdi32.lib Ws2_32.lib" )
gcc_bin_ldflags( "medfall", game_ldflags )
-bin( "launch", { "launcher/main", "http", "sha2", "patterns", "platform_network", game_objs }, { "imgui", "monocypher", "whereami", game_libs } )
+bin( "launch", { "launcher/main", "http", "sha2", "platform_network", game_objs }, { "imgui", "monocypher", "whereami", game_libs } )
msvc_bin_ldflags( "launch", "opengl32.lib gdi32.lib Ws2_32.lib" )
gcc_bin_ldflags( "launch", game_ldflags )
diff --git a/platform_home.h b/platform_home.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "platform.h"
+
+#if PLATFORM_WINDOWS
+#include "win32_home.h"
+#elif PLATFORM_UNIX
+#include "unix_home.h"
+#else
+#error new platform
+#endif
diff --git a/unix_home.h b/unix_home.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <stdlib.h>
+
+#include "dynstr.h"
+
+inline void get_config_directory( DynamicString & path ) {
+ const char * home = getenv( "HOME" );
+ if( home == NULL ) {
+ FATAL( "can't read HOME environment variable" );
+ }
+ path.sprintf( "{}/.medfall", home );
+}
diff --git a/win32_home.h b/win32_home.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <shlobj.h>
+
+#include "dynstr.h"
+
+inline void get_config_directory( DynamicString & path ) {
+ char appdata[ PATH_MAX ];
+ if( SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, 0, appdata ) != S_OK ) {
+ FATAL( "can't find AppData" );
+ }
+ path.printf( "{}\\Medfall", appdata );
+}