Skip to content

Commit

Permalink
support clang __FILE_NAME__ macro
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Nov 17, 2020
1 parent 115fd54 commit 2515a63
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
10 changes: 4 additions & 6 deletions Core/MMKVLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ MMKV_NAMESPACE_END

using namespace mmkv;

# ifndef __FILE_NAME__
const char *_getFileName(const char *path) {
const char *ptr = strrchr(path, '/');
if (!ptr) {
Expand All @@ -49,6 +50,7 @@ const char *_getFileName(const char *path) {
return path;
}
}
# endif

# ifndef MMKV_ANDROID

Expand All @@ -69,16 +71,14 @@ static const char *MMKVLogLevelDesc(MMKVLogLevel level) {

# ifdef MMKV_APPLE

void _MMKVLogWithLevel(MMKVLogLevel level, const char *file, const char *func, int line, const char *format, ...) {
void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
if (level >= g_currentLogLevel) {
NSString *nsFormat = [NSString stringWithUTF8String:format];
va_list argList;
va_start(argList, format);
NSString *message = [[NSString alloc] initWithFormat:nsFormat arguments:argList];
va_end(argList);

auto filename = _getFileName(file);

if (g_logHandler) {
g_logHandler(level, filename, line, func, message);
} else {
Expand All @@ -89,7 +89,7 @@ void _MMKVLogWithLevel(MMKVLogLevel level, const char *file, const char *func, i

# else

void _MMKVLogWithLevel(MMKVLogLevel level, const char *file, const char *func, int line, const char *format, ...) {
void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
if (level >= g_currentLogLevel) {
std::string message;
char buffer[16];
Expand All @@ -110,8 +110,6 @@ void _MMKVLogWithLevel(MMKVLogLevel level, const char *file, const char *func, i
va_end(args);
}

auto filename = _getFileName(file);

if (g_logHandler) {
g_logHandler(level, filename, line, func, message);
} else {
Expand Down
21 changes: 16 additions & 5 deletions Core/MMKVLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <cstring>

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

MMKV_NAMESPACE_BEGIN

Expand All @@ -41,16 +41,27 @@ extern mmkv::LogHandler g_logHandler;

#ifdef ENABLE_MMKV_LOG

# ifdef __FILE_NAME__
# define __MMKV_FILE_NAME__ __FILE_NAME__
# else
const char *_getFileName(const char *path);
# define __MMKV_FILE_NAME__ _getFileName(__FILE__)
# endif

# define MMKVError(format, ...) \
_MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogError, __FILE__, __func__, __LINE__, format, ##__VA_ARGS__)
_MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogError, __MMKV_FILE_NAME__, __func__, __LINE__, format, \
##__VA_ARGS__)
# define MMKVWarning(format, ...) \
_MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogWarning, __FILE__, __func__, __LINE__, format, ##__VA_ARGS__)
_MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogWarning, __MMKV_FILE_NAME__, __func__, __LINE__, format, \
##__VA_ARGS__)
# define MMKVInfo(format, ...) \
_MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogInfo, __FILE__, __func__, __LINE__, format, ##__VA_ARGS__)
_MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogInfo, __MMKV_FILE_NAME__, __func__, __LINE__, format, \
##__VA_ARGS__)

# ifdef MMKV_DEBUG
# define MMKVDebug(format, ...) \
_MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogDebug, __FILE__, __func__, __LINE__, format, ##__VA_ARGS__)
_MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogDebug, __MMKV_FILE_NAME__, __func__, __LINE__, format, \
##__VA_ARGS__)
# else
# define MMKVDebug(format, ...) \
{}
Expand Down

0 comments on commit 2515a63

Please sign in to comment.