[logging] Abort app if LOG error level is greater than LERROR (debug) or LCRITICAL (release).

This commit is contained in:
vng 2015-09-25 14:19:09 +03:00
parent c97073ff16
commit 7629852c75
5 changed files with 43 additions and 79 deletions

View file

@ -1,8 +1,5 @@
#include "logging.hpp"
#include <android/log.h>
#include <cassert>
#include "base/exception.hpp"
#include "base/logging.hpp"
@ -11,17 +8,21 @@
#include "platform/file_logging.hpp"
#include "platform/platform.hpp"
#include <android/log.h>
#include <cassert>
#include <cstdlib>
namespace jni
{
using namespace my;
void AndroidLogMessage(LogLevel l, SrcPoint const & src, string const & s)
void AndroidMessage(LogLevel level, SrcPoint const & src, string const & s)
{
android_LogPriority pr = ANDROID_LOG_SILENT;
switch (l)
switch (level)
{
case LINFO: pr = ANDROID_LOG_INFO; break;
case LDEBUG: pr = ANDROID_LOG_DEBUG; break;
@ -34,24 +35,30 @@ void AndroidLogMessage(LogLevel l, SrcPoint const & src, string const & s)
__android_log_write(pr, "MapsWithMe_JNI", out.c_str());
}
void AndroidLogMessage(LogLevel level, SrcPoint const & src, string const & s)
{
AndroidMessage(level, src, s);
CHECK(level < g_LogAbortLevel, ("Abort. Log level is too serious", level));
}
void AndroidAssertMessage(SrcPoint const & src, string const & s)
{
#if defined(MWM_LOG_TO_FILE)
LogMessageFile(LERROR, src, s);
#ifdef MWM_LOG_TO_FILE
LogMessageFile(LCRITICAL, src, s);
#else
AndroidLogMessage(LERROR, src, s);
AndroidMessage(LCRITICAL, src, s);
#endif
#ifdef DEBUG
assert(false);
assert(false);
#else
MYTHROW(RootException, (s));
std::abort();
#endif
}
void InitSystemLog()
{
#if defined(MWM_LOG_TO_FILE)
#ifdef MWM_LOG_TO_FILE
SetLogMessageFn(&LogMessageFile);
#else
SetLogMessageFn(&AndroidLogMessage);

View file

@ -6,29 +6,21 @@
#include "std/iostream.hpp"
#include <cassert>
#include <cstdlib>
#ifdef OMIM_OS_TIZEN
#include <FBaseSys.h>
#endif
namespace my
{
void OnAssertFailedDefault(SrcPoint const & srcPoint, string const & msg)
{
#ifdef OMIM_OS_TIZEN
AppLog("ASSERT FAILED%s:%d:%s", srcPoint.FileName(), srcPoint.Line(), msg.c_str());
AppAssert(false);
#else
std::cerr << "ASSERT FAILED\n" << srcPoint.FileName() << ":" << srcPoint.Line() << "\n"
<< msg << endl;
std::cerr << "ASSERT FAILED" << endl
<< srcPoint.FileName() << ":" << srcPoint.Line() << endl
<< msg << endl;
#ifdef DEBUG
assert(false);
#else
MYTHROW(RootException, (msg));
#endif
std::abort();
#endif
}

View file

@ -1,12 +1,12 @@
#include "base/assert.hpp"
#include "base/logging.hpp"
#include "base/macros.hpp"
#include "base/timer.hpp"
#include "base/thread.hpp"
#include "base/mutex.hpp"
#include "base/thread.hpp"
#include "base/timer.hpp"
#include "std/iostream.hpp"
#include "std/iomanip.hpp"
#include "std/iostream.hpp"
#include "std/mutex.hpp"
#include "std/sstream.hpp"
#include "std/target_os.hpp"
@ -15,41 +15,6 @@
namespace my
{
void LogCheckIfErrorLevel(LogLevel level)
{
#ifdef DEBUG
if (level >= LERROR)
#else
if (level >= LCRITICAL)
#endif
{
CHECK(false, ("Error level is too serious", level));
}
}
#ifdef OMIM_OS_TIZEN
#include <FBaseLog.h>
void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg)
{
ostringstream out;
out << DebugPrint(srcPoint) << msg << endl;
switch (level)
{
case LDEBUG:
AppLogDebug(out.str().c_str());
break;
case LINFO:
case LWARNING:
AppLog(out.str().c_str());
break;
case LERROR:
case LCRITICAL:
AppLogException(out.str().c_str());
}
}
#else
class LogHelper
{
int m_threadsCount;
@ -69,8 +34,6 @@ namespace my
size_t m_lens[5];
public:
threads::Mutex m_mutex;
LogHelper() : m_threadsCount(0)
{
m_names[0] = "DEBUG"; m_lens[0] = 5;
@ -92,39 +55,36 @@ namespace my
}
};
mutex g_logMutex;
void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg)
{
static LogHelper logger;
lock_guard<mutex> lock(g_logMutex);
UNUSED_VALUE(lock);
threads::MutexGuard guard(logger.m_mutex);
UNUSED_VALUE(guard);
static LogHelper logger;
ostringstream out;
logger.WriteProlog(out, level);
out << DebugPrint(srcPoint) << msg << endl;
std::cerr << out.str();
CHECK(level < g_LogAbortLevel, ("Abort. Log level is too serious", level));
}
void LogMessageTests(LogLevel level, SrcPoint const & srcPoint, string const & msg)
void LogMessageTests(LogLevel level, SrcPoint const &, string const & msg)
{
static mutex mtx;
lock_guard<mutex> lock(mtx);
lock_guard<mutex> lock(g_logMutex);
UNUSED_VALUE(lock);
ostringstream out;
out << msg << endl;
std::cerr << out.str();
#ifdef OMIM_OS_WINDOWS
OutputDebugStringA(out.str().c_str());
#endif
LogCheckIfErrorLevel(level);
CHECK(level < g_LogAbortLevel, ("Abort. Log level is too serious", level));
}
#endif
LogMessageFn LogMessage = &LogMessageDefault;
LogMessageFn SetLogMessageFn(LogMessageFn fn)
@ -135,7 +95,9 @@ namespace my
#ifdef DEBUG
LogLevel g_LogLevel = LDEBUG;
LogLevel g_LogAbortLevel = LERROR;
#else
LogLevel g_LogLevel = LINFO;
LogLevel g_LogAbortLevel = LCRITICAL;
#endif
}

View file

@ -18,9 +18,12 @@ namespace my
extern LogMessageFn LogMessage;
extern LogLevel g_LogLevel;
extern LogLevel g_LogAbortLevel;
/// @return Pointer to previous message function.
LogMessageFn SetLogMessageFn(LogMessageFn fn);
void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg);
void LogMessageTests(LogLevel level, SrcPoint const & srcPoint, string const & msg);
}

View file

@ -182,7 +182,7 @@ ModelReader * Platform::GetReader(string const & file, string const & searchScop
}
}
LOG(LERROR, ("Can't get reader for:", file));
LOG(LWARNING, ("Can't get reader for:", file));
MYTHROW(FileAbsentException, ("File not found", file));
return 0;
}