update log.c, bump version number

This commit is contained in:
zeffy
2018-04-02 08:48:37 -07:00
parent c2069e3c69
commit 220b2eeaff
4 changed files with 41 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
version: 1.0.0.{build} version: 1.0.1.{build}
branches: branches:
only: only:
- master - master

View File

@@ -3,54 +3,55 @@
#include <ShlObj.h> #include <ShlObj.h>
HANDLE m_hFile = INVALID_HANDLE_VALUE; static HANDLE m_hFile = INVALID_HANDLE_VALUE;
static DWORD m_dwProcessId;
static wchar_t m_szExeFilePath[MAX_PATH];
static wchar_t *m_pszExeName;
BOOL CALLBACK init_file_handle( BOOL CALLBACK init_once_callback(
PINIT_ONCE pInitOnce, PINIT_ONCE InitOnce,
ParamData *pParam, PVOID *Parameter,
PVOID *ppContext) PVOID *lpContext)
{ {
BOOL result = FALSE; BOOL result;
HANDLE hFile;
HRESULT hr; HRESULT hr;
wchar_t *pszPath; wchar_t *pszPath;
wchar_t szFilePath[MAX_PATH]; wchar_t szFilePath[MAX_PATH];
int ret; int ret;
pParam->dwProcessId = GetCurrentProcessId(); m_dwProcessId = GetCurrentProcessId();
if ( !GetModuleFileNameW(NULL, pParam->szExeFilePath, _countof(pParam->szExeFilePath)) ) { if ( !GetModuleFileNameW(NULL, m_szExeFilePath, _countof(m_szExeFilePath)) ) {
log_debug(L"GetModuleFileNameW failed! (GLE=%lu)", GetLastError()); log_debug(L"GetModuleFileNameW failed! (GLE=%lu)", GetLastError());
return result; return FALSE;
} }
pParam->pszExeName = PathFindFileNameW(pParam->szExeFilePath); m_pszExeName = PathFindFileNameW(m_szExeFilePath);
hr = SHGetKnownFolderPath(&FOLDERID_ProgramData, 0, NULL, &pszPath); hr = SHGetKnownFolderPath(&FOLDERID_ProgramData, 0, NULL, &pszPath);
if ( hr != S_OK ) { if ( hr != S_OK ) {
log_debug(L"SHGetKnownFolderPath failed! (HRESULT=0x%08X)", hr); log_debug(L"SHGetKnownFolderPath failed! (HRESULT=0x%08X)", hr);
return result; return FALSE;
} }
ret = wcscpy_s(szFilePath, _countof(szFilePath), pszPath); ret = wcscpy_s(szFilePath, _countof(szFilePath), pszPath);
CoTaskMemFree(pszPath); CoTaskMemFree(pszPath);
if ( ret ) { if ( ret ) {
log_debug(L"wcscpy_s failed! (Return value=%d)", ret); log_debug(L"wcscpy_s failed! (Return value=%d)", ret);
return result; return FALSE;
} }
if ( !PathAppendW(szFilePath, L"wufuc") ) { if ( !PathAppendW(szFilePath, L"wufuc") ) {
append_fail: append_fail:
log_debug(L"PathAppendW failed!"); log_debug(L"PathAppendW failed!");
return result; return FALSE;
} }
if ( !CreateDirectoryW(szFilePath, NULL) if ( !CreateDirectoryW(szFilePath, NULL)
&& GetLastError() != ERROR_ALREADY_EXISTS ) { && GetLastError() != ERROR_ALREADY_EXISTS ) {
log_debug(L"CreateDirectoryW failed! (GLE=%lu)", GetLastError()); log_debug(L"CreateDirectoryW failed! (GLE=%lu)", GetLastError());
return result; return FALSE;
} }
if ( !PathAppendW(szFilePath, L"wufuc.log") ) if ( !PathAppendW(szFilePath, L"wufuc.1.log") )
goto append_fail; goto append_fail;
hFile = CreateFileW(szFilePath, m_hFile = CreateFileW(szFilePath,
FILE_APPEND_DATA, FILE_APPEND_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, NULL,
@@ -58,12 +59,9 @@ append_fail:
FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL,
NULL); NULL);
if ( hFile != INVALID_HANDLE_VALUE ) { result = m_hFile != INVALID_HANDLE_VALUE;
*ppContext = (PVOID)hFile; if ( !result )
result = TRUE;
} else {
log_debug(L"CreateFileW failed! (GLE=%lu)", GetLastError()); log_debug(L"CreateFileW failed! (GLE=%lu)", GetLastError());
}
return result; return result;
} }
@@ -93,9 +91,7 @@ void log_debug_(const wchar_t *const format, ...)
void log_trace_(const wchar_t *const format, ...) void log_trace_(const wchar_t *const format, ...)
{ {
static INIT_ONCE InitOnce = INIT_ONCE_STATIC_INIT; static INIT_ONCE InitOnce = INIT_ONCE_STATIC_INIT;
static ParamData data;
BOOL bStatus; BOOL bStatus;
errno_t e;
wchar_t datebuf[9]; wchar_t datebuf[9];
wchar_t timebuf[9]; wchar_t timebuf[9];
va_list ap; va_list ap;
@@ -108,46 +104,44 @@ void log_trace_(const wchar_t *const format, ...)
char *buf3; char *buf3;
DWORD written; DWORD written;
bStatus = InitOnceExecuteOnce(&InitOnce, bStatus = InitOnceExecuteOnce(&InitOnce, init_once_callback, NULL, NULL);
(PINIT_ONCE_FN)init_file_handle,
&data,
&(LPVOID)m_hFile);
e = _wstrdate_s(datebuf, _countof(datebuf)); if ( _wstrdate_s(datebuf, _countof(datebuf))
if ( e ) return; || _wstrtime_s(timebuf, _countof(timebuf)) )
e = _wstrtime_s(timebuf, _countof(timebuf)); return;
if ( e ) return;
va_start(ap, format); va_start(ap, format);
count = _vscwprintf(format, ap); ret = _vscwprintf(format, ap);
va_end(ap); va_end(ap);
if ( count == -1 ) return; if ( ret == -1 ) return;
count = ret + 1;
buf1 = calloc(count + 1, sizeof *buf1); buf1 = calloc(count, sizeof *buf1);
if ( !buf1 ) return; if ( !buf1 ) return;
va_start(ap, format); va_start(ap, format);
ret = vswprintf_s(buf1, count + 1, format, ap); ret = vswprintf_s(buf1, count, format, ap);
va_end(ap); va_end(ap);
if ( ret == -1 ) goto free_buf1; if ( ret == -1 ) goto free_buf1;
count = _scwprintf(fmt, datebuf, timebuf, data.pszExeName, data.dwProcessId, buf1); ret = _scwprintf(fmt, datebuf, timebuf, m_pszExeName, m_dwProcessId, buf1);
if ( count == -1 ) goto free_buf1; if ( ret == -1 ) goto free_buf1;
count = ret + 1;
buf2 = calloc(count + 1, sizeof *buf2); buf2 = calloc(count, sizeof *buf2);
if ( !buf2 ) goto free_buf1; if ( !buf2 ) goto free_buf1;
count = swprintf_s(buf2, count + 1, fmt, datebuf, timebuf, data.pszExeName, data.dwProcessId, buf1); ret = swprintf_s(buf2, count, fmt, datebuf, timebuf, m_pszExeName, m_dwProcessId, buf1);
if ( count == -1 ) goto free_buf2; if ( ret == -1 ) goto free_buf2;
if ( bStatus ) { if ( bStatus ) {
size = WideCharToMultiByte(CP_UTF8, 0, buf2, count, NULL, 0, NULL, NULL); size = WideCharToMultiByte(CP_UTF8, 0, buf2, ret, NULL, 0, NULL, NULL);
if ( !size ) goto fallback; if ( !size ) goto fallback;
buf3 = malloc(size); buf3 = malloc(size);
if ( !buf3 ) goto fallback; if ( !buf3 ) goto fallback;
ret = WideCharToMultiByte(CP_UTF8, 0, buf2, count, buf3, size, NULL, NULL) ret = WideCharToMultiByte(CP_UTF8, 0, buf2, ret, buf3, size, NULL, NULL)
&& WriteFile(m_hFile, buf3, size, &written, NULL); && WriteFile(m_hFile, buf3, size, &written, NULL);
free(buf3); free(buf3);
if ( !ret ) goto fallback; if ( !ret ) goto fallback;

View File

@@ -1,12 +1,5 @@
#pragma once #pragma once
typedef struct
{
DWORD dwProcessId;
wchar_t szExeFilePath[MAX_PATH];
wchar_t *pszExeName;
} ParamData;
void log_debug_(const wchar_t *const format, ...); void log_debug_(const wchar_t *const format, ...);
void log_trace_(const wchar_t *const format, ...); void log_trace_(const wchar_t *const format, ...);
void log_close(void); void log_close(void);

View File

@@ -1,11 +1,11 @@
#pragma once #pragma once
#ifndef BUILD_COMMIT_VERSION #ifndef BUILD_COMMIT_VERSION
#define BUILD_COMMIT_VERSION 1.0.0.0 #define BUILD_COMMIT_VERSION 1.0.1.0
#endif #endif
#ifndef BUILD_VERSION_COMMA #ifndef BUILD_VERSION_COMMA
#define BUILD_VERSION_COMMA 1,0,0,0 #define BUILD_VERSION_COMMA 1,0,1,0
#endif #endif
#define S_(x) #x #define S_(x) #x