Skip to content

Commit

Permalink
redirect logging on Win32 & protect from meta file corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Jan 22, 2019
1 parent 5b4cec4 commit 969b784
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 8 deletions.
16 changes: 16 additions & 0 deletions Win32/MMKV/MMKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static unordered_map<std::string, MMKV *> *g_instanceDic;
static ThreadLock g_instanceLock;
static std::wstring g_rootDir;
static MMKV::ErrorHandler g_errorHandler;
MMKV::LogHandler g_logHandler;

#define DEFAULT_MMAP_ID "mmkv.default"
#define SPECIAL_CHARACTER_DIRECTORY_NAME L"specialCharacter"
Expand Down Expand Up @@ -1218,6 +1219,21 @@ void MMKV::unRegisetErrorHandler() {
g_errorHandler = nullptr;
}

void MMKV::regiserLogHandler(LogHandler handler) {
SCOPEDLOCK(g_instanceLock);
g_logHandler = handler;
}

void MMKV::unRegisetLogHandler() {
SCOPEDLOCK(g_instanceLock);
g_logHandler = nullptr;
}

void MMKV::setLogLevel(MMKVLogLevel level) {
SCOPEDLOCK(g_instanceLock);
g_currentLogLevel = level;
}

static void mkSpecialCharacterFileDirectory() {
wstring path = g_rootDir + L"/" + SPECIAL_CHARACTER_DIRECTORY_NAME;
mkPath(path);
Expand Down
18 changes: 18 additions & 0 deletions Win32/MMKV/MMKV.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ enum MMKVErrorType : int {
MMKVFileLength,
};

enum MMKVLogLevel : int {
MMKVLogDebug = 0, // not available for release/product build
MMKVLogInfo = 1, // default level
MMKVLogWarning,
MMKVLogError,
MMKVLogNone, // special level used to disable all log messages
};

class MMKV {
std::unordered_map<std::string, mmkv::MMBuffer> m_dic;
std::string m_mmapID;
Expand Down Expand Up @@ -239,6 +247,16 @@ class MMKV {
MMKVErrorType errorType);
static void regiserErrorHandler(ErrorHandler handler);
static void unRegisetErrorHandler();

typedef void (*LogHandler)(MMKVLogLevel level,
const std::string &file,
int line,
const std::string &function,
const std::string &message);
static void regiserLogHandler(LogHandler handler);
static void unRegisetLogHandler();

static void setLogLevel(MMKVLogLevel level);
};

#endif // MMKV_MMKV_H
3 changes: 2 additions & 1 deletion Win32/MMKV/MMKV.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,12 @@
<ClCompile Include="MmapedFile.cpp" />
<ClCompile Include="MMBuffer.cpp" />
<ClCompile Include="MMKV.cpp" />
<ClCompile Include="MMKVLog.cpp" />
<ClCompile Include="PBUtility.cpp" />
<ClCompile Include="pch.cpp" />
<ClCompile Include="ThreadLock.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
3 changes: 3 additions & 0 deletions Win32/MMKV/MMKV.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,8 @@
<ClCompile Include="pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MMKVLog.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
79 changes: 79 additions & 0 deletions Win32/MMKV/MMKVLog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "MMKVLog.h"

#ifdef ENABLE_MMKV_LOG

using namespace std;

#ifndef NDEBUG
MMKVLogLevel g_currentLogLevel = MMKVLogDebug;
#else
MMKVLogLevel g_currentLogLevel = MMKVLogInfo;
#endif

static const char *MMKVLogLevelDesc(MMKVLogLevel level) {
switch (level) {
case MMKVLogDebug:
return "D";
case MMKVLogInfo:
return "I";
case MMKVLogWarning:
return "W";
case MMKVLogError:
return "E";
default:
return "N";
}
}

void _MMKVLogWithLevel(
MMKVLogLevel level, const char *file, const char *func, int line, const char *format, ...) {
if (level >= g_currentLogLevel) {
string message;
char buffer[16];

va_list args;
va_start(args, format);
auto length = std::vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);

if (length < 0) { // something wrong
message = {};
} else if (length < sizeof(buffer)) {
message = string(buffer, length);
} else {
message.resize(length, '\0');
va_start(args, format);
std::vsnprintf(const_cast<char *>(message.data()), length + 1, format, args);
va_end(args);
}

if (g_logHandler) {
g_logHandler(level, file, line, func, message);
} else {
printf("[%s] <%s:%d::%s> %s\n", MMKVLogLevelDesc(level), file, line, func,
message.c_str());
}
}
}

#endif // ENABLE_MMKV_LOG
23 changes: 16 additions & 7 deletions Win32/MMKV/MMKVLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef MMKV_MMKVLOG_H
#define MMKV_MMKVLOG_H

#include "MMKV.h"
#include <cstdio>
#include <cstring>

Expand All @@ -29,20 +30,28 @@

#ifdef ENABLE_MMKV_LOG

#define APPNAME "MMKV"
extern MMKVLogLevel g_currentLogLevel;
extern MMKV::LogHandler g_logHandler;

#define __NEW_LINE "\n"
#define __filename__ (strrchr(__FILE__, '\\') + 1)

#define MMKVError(format, ...) ::printf(format __NEW_LINE, ##__VA_ARGS__)
#define MMKVWarning(format, ...) ::printf(format __NEW_LINE, ##__VA_ARGS__)
#define MMKVInfo(format, ...) ::printf(format __NEW_LINE, ##__VA_ARGS__)
#define MMKVError(format, ...) \
_MMKVLogWithLevel(MMKVLogError, __filename__, __func__, __LINE__, format, ##__VA_ARGS__)
#define MMKVWarning(format, ...) \
_MMKVLogWithLevel(MMKVLogWarning, __filename__, __func__, __LINE__, format, ##__VA_ARGS__)
#define MMKVInfo(format, ...) \
_MMKVLogWithLevel(MMKVLogInfo, __filename__, __func__, __LINE__, format, ##__VA_ARGS__)

#ifndef NDEBUG
#define MMKVDebug(format, ...) ::printf(format __NEW_LINE, ##__VA_ARGS__)
#define MMKVDebug(format, ...) \
_MMKVLogWithLevel(MMKVLogDebug, __filename__, __func__, __LINE__, format, ##__VA_ARGS__)
#else
#define MMKVDebug(format, ...) \
{}
#endif
#endif // NDEBUG

void _MMKVLogWithLevel(
MMKVLogLevel level, const char *file, const char *func, int line, const char *format, ...);

#else

Expand Down
6 changes: 6 additions & 0 deletions Win32/MMKV/MmapedFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

#include "pch.h"

#include "InterProcessLock.h"
#include "MMBuffer.h"
#include "MMKVLog.h"
#include "MmapedFile.h"
#include "ScopedLock.hpp"

using namespace std;

Expand All @@ -47,6 +49,10 @@ MmapedFile::MmapedFile(const std::wstring &path, size_t size)
if (m_file == INVALID_HANDLE_VALUE) {
MMKVError("fail to open:%ws, %d", m_name.c_str(), GetLastError());
} else {
FileLock fileLock(m_file);
InterProcessLock lock(&fileLock, ExclusiveLockType);
SCOPEDLOCK(lock);

getfilesize(m_file, m_segmentSize);
if (m_segmentSize < DEFAULT_MMAP_SIZE) {
m_segmentSize = static_cast<size_t>(DEFAULT_MMAP_SIZE);
Expand Down
26 changes: 26 additions & 0 deletions Win32/Win32Demo/Win32Demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,39 @@ void processTest() {
cout << "total count of process_test: " << mmkv->count() << endl;
}

static void LogHandler(MMKVLogLevel level,
const std::string &file,
int line,
const std::string &function,
const std::string &message) {

auto desc = [level] {
switch (level) {
case MMKVLogDebug:
return "D";
case MMKVLogInfo:
return "I";
case MMKVLogWarning:
return "W";
case MMKVLogError:
return "E";
default:
return "N";
}
}();
printf("redirecting-[%s] <%s:%d::%s> %s\n", desc, file.c_str(), line, function.c_str(),
message.c_str());
}

int main() {
locale::global(locale(""));
wcout.imbue(locale(""));
srand(GetTickCount());

wstring rootDir = getAppDataRoaming(L"Tencent", L"΢ÐÅ-MMKV");
MMKV::initializeMMKV(rootDir);
//MMKV::setLogLevel(MMKVLogNone);
MMKV::regiserLogHandler(LogHandler);

auto mmkv = MMKV::defaultMMKV();
functionalTest(mmkv, false);
Expand Down

0 comments on commit 969b784

Please sign in to comment.