-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from lonemeow/improve-logs
Add support for saving logs to file
- Loading branch information
Showing
8 changed files
with
87 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Diagnostics; | ||
|
||
namespace ACCConnector { | ||
internal class Logging { | ||
public enum Severity { | ||
FATAL, | ||
ERROR, | ||
WARNING, | ||
INFO, | ||
DEBUG | ||
} | ||
|
||
private static StreamWriter? logStream; | ||
|
||
public static void Init(string logFolder) { | ||
Directory.CreateDirectory(logFolder); | ||
logStream = new StreamWriter(File.Open(Path.Join(logFolder, "app.log"), FileMode.Append, FileAccess.Write, FileShare.Read)) { | ||
AutoFlush = true | ||
}; | ||
} | ||
|
||
public static void Log(Severity severity, string msg) { | ||
logStream?.WriteLine($"{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff} {severity}: {msg}"); | ||
Trace.WriteLine(msg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ void log_msg(const wchar_t* fmt, ...); | |
|
||
BOOL initProxy(); | ||
void closeProxy(); | ||
|
||
void initLog(); | ||
void closeLog(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,48 @@ | ||
#include "client-hooks.h" | ||
|
||
#include <Shlobj.h> | ||
#include <Share.h> | ||
#include <stdio.h> | ||
|
||
FILE* log_file = NULL; | ||
|
||
void initLog() { | ||
wchar_t *documents_path; | ||
HRESULT res = SHGetKnownFolderPath(&FOLDERID_Documents, 0, NULL, &documents_path); | ||
if (SUCCEEDED(res)) { | ||
wchar_t path[512]; | ||
swprintf_s(path, 512, L"%s\\ACC Connector", documents_path); | ||
CreateDirectory(path, NULL); | ||
swprintf_s(path, 512, L"%s\\ACC Connector\\logs", documents_path); | ||
CreateDirectory(path, NULL); | ||
swprintf_s(path, 512, L"%s\\ACC Connector\\logs\\hook.log", documents_path); | ||
log_file = _wfsopen(path, L"a", _SH_DENYWR); | ||
CoTaskMemFree(documents_path); | ||
log_msg(L"Log opened"); | ||
} | ||
else { | ||
log_msg(L"Failed to get Documents folder location: %x", res); | ||
} | ||
} | ||
|
||
void closeLog() { | ||
log_msg(L"Closing log"); | ||
fclose(log_file); | ||
log_file = NULL; | ||
} | ||
|
||
void log_msg(const wchar_t* fmt, ...) { | ||
va_list args; | ||
wchar_t buffer[512]; | ||
va_start(args, fmt); | ||
vswprintf_s(buffer, 512, fmt, args); | ||
va_end(args); | ||
OutputDebugStringW(buffer); | ||
if (log_file) { | ||
SYSTEMTIME time; | ||
GetLocalTime(&time); | ||
fwprintf(log_file, L"%04d-%02d-%02d %02d:%02d:%02d.%03d %s\n", | ||
time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, buffer); | ||
fflush(log_file); | ||
} | ||
} |