From 7e8f0172e81bd448c8c2419b66a2881d85a89c62 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Wed, 18 Mar 2015 09:04:06 +0300 Subject: [PATCH] Fixes after colleagues comments --- android/jni/com/mapswithme/core/logging.cpp | 6 +- .../jni/com/mapswithme/maps/MapFragment.cpp | 2 +- .../jni/com/mapswithme/platform/Platform.cpp | 25 +++----- .../com/mapswithme/util/log/MemLogging.java | 26 ++++---- iphone/Maps/Classes/MapViewController.mm | 11 ++-- iphone/Maps/main.mm | 4 +- platform/file_logging.cpp | 60 +++++++++++-------- platform/file_logging.hpp | 19 +++++- platform/platform.hpp | 4 +- platform/platform.pro | 26 ++++---- platform/platform_ios.mm | 13 ++-- std/chrono.hpp | 2 - 12 files changed, 109 insertions(+), 89 deletions(-) diff --git a/android/jni/com/mapswithme/core/logging.cpp b/android/jni/com/mapswithme/core/logging.cpp index 0539454224..e640cf95c9 100644 --- a/android/jni/com/mapswithme/core/logging.cpp +++ b/android/jni/com/mapswithme/core/logging.cpp @@ -3,11 +3,13 @@ #include #include -#include "../../../../../base/logging.hpp" #include "../../../../../base/exception.hpp" +#include "../../../../../base/logging.hpp" + #include "../../../../../coding/file_writer.hpp" -#include "../../../../../platform/platform.hpp" + #include "../../../../../platform/file_logging.hpp" +#include "../../../../../platform/platform.hpp" namespace jni diff --git a/android/jni/com/mapswithme/maps/MapFragment.cpp b/android/jni/com/mapswithme/maps/MapFragment.cpp index 5e90487ad5..dbe834e1ea 100644 --- a/android/jni/com/mapswithme/maps/MapFragment.cpp +++ b/android/jni/com/mapswithme/maps/MapFragment.cpp @@ -54,7 +54,7 @@ extern "C" if (speed > 0.0) info.m_speed = speed; - LogMemoryInfo(); + LOG_MEMORY_INFO(); g_framework->OnLocationUpdated(info); } diff --git a/android/jni/com/mapswithme/platform/Platform.cpp b/android/jni/com/mapswithme/platform/Platform.cpp index 2e3a891583..0e13f6cc6d 100644 --- a/android/jni/com/mapswithme/platform/Platform.cpp +++ b/android/jni/com/mapswithme/platform/Platform.cpp @@ -19,10 +19,7 @@ string Platform::UniqueClientId() const { JNIEnv * env = jni::GetEnv(); if (!env) - { - LOG(LWARNING, ("Can't get JNIEnv")); - return ""; - } + return string(); jclass uuidClass = env->FindClass("java/util/UUID"); ASSERT(uuidClass, ("Can't find java class java/util/UUID")); @@ -58,31 +55,25 @@ string Platform::UniqueClientId() const string Platform::GetMemoryInfo() const { JNIEnv * env = jni::GetEnv(); - if (!env) - { - LOG(LWARNING, ("Can't get JNIEnv")); - return string(""); - } + if (env == nullptr) + return string(); - jclass memLoggingClass = env->FindClass("com/mapswithme/util/log/MemLogging"); - ASSERT(memLoggingClass, ()); + static shared_ptr classMemLogging = jni::make_global_ref(env->FindClass("com/mapswithme/util/log/MemLogging")); + ASSERT(classMemLogging, ()); - jmethodID getMemoryInfoId = env->GetStaticMethodID(memLoggingClass, "GetMemoryInfo", "()Ljava/lang/String;"); + static jmethodID const getMemoryInfoId = env->GetStaticMethodID(static_cast(*classMemLogging.get()), "getMemoryInfo", "()Ljava/lang/String;"); ASSERT(getMemoryInfoId, ()); - jstring memInfoString = (jstring)env->CallStaticObjectMethod(memLoggingClass, getMemoryInfoId); + jstring const memInfoString = (jstring)env->CallStaticObjectMethod(static_cast(*classMemLogging.get()), getMemoryInfoId); ASSERT(memInfoString, ()); - string res = jni::ToNativeString(env, memInfoString); - - return res; + return jni::ToNativeString(env, memInfoString); } void Platform::RunOnGuiThread(TFunctor const & fn) { android::Platform::RunOnGuiThreadImpl(fn); } - namespace android { void Platform::Initialize(JNIEnv * env, diff --git a/android/src/com/mapswithme/util/log/MemLogging.java b/android/src/com/mapswithme/util/log/MemLogging.java index 1800657150..efc281ecf9 100644 --- a/android/src/com/mapswithme/util/log/MemLogging.java +++ b/android/src/com/mapswithme/util/log/MemLogging.java @@ -11,27 +11,27 @@ import java.util.Date; public class MemLogging { - public static String GetMemoryInfo() + public static String getMemoryInfo() { final Debug.MemoryInfo debugMI = new Debug.MemoryInfo(); Debug.getMemoryInfo(debugMI); final ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); - final Context content = MWMApplication.get().getApplicationContext(); final ActivityManager activityManager = - (ActivityManager) content.getSystemService(content.ACTIVITY_SERVICE); + (ActivityManager) MWMApplication.get().getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); activityManager.getMemoryInfo(mi); - StringBuilder log = new StringBuilder("Memory info: " + - " Debug.getNativeHeapSize() = " + Debug.getNativeHeapSize() / 1024 + - "KB; Debug.getNativeHeapAllocatedSize() = " + Debug.getNativeHeapAllocatedSize() / 1024 + - "KB; Debug.getNativeHeapFreeSize() = " + Debug.getNativeHeapFreeSize() / 1024 + - "KB; debugMI.getTotalPrivateDirty() = " + debugMI.getTotalPrivateDirty() + - "KB; debugMI.getTotalPss() = " + debugMI.getTotalPss() + - "KB; mi.availMem = " + mi.availMem / 1024 + - "KB; mi.threshold = " + mi.threshold / 1024 + - "KB; mi.lowMemory = " + mi.lowMemory); + + StringBuilder log = new StringBuilder("Memory info: "); + log.append(" Debug.getNativeHeapSize() = ").append(Debug.getNativeHeapSize() / 1024) + .append("KB; Debug.getNativeHeapAllocatedSize() = ").append(Debug.getNativeHeapAllocatedSize() / 1024) + .append("KB; Debug.getNativeHeapFreeSize() = ").append(Debug.getNativeHeapFreeSize() / 1024) + .append("KB; debugMI.getTotalPrivateDirty() = ").append(debugMI.getTotalPrivateDirty()) + .append("KB; debugMI.getTotalPss() = ").append(debugMI.getTotalPss()) + .append("KB; mi.availMem = ").append(mi.availMem / 1024) + .append("KB; mi.threshold = ").append(mi.threshold / 1024) + .append("KB; mi.lowMemory = ").append(mi.lowMemory); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - log.append(" mi.totalMem = " + mi.totalMem / 1024 + "KB."); + log.append(" mi.totalMem = ").append(mi.totalMem / 1024).append("KB;"); } return log.toString(); } diff --git a/iphone/Maps/Classes/MapViewController.mm b/iphone/Maps/Classes/MapViewController.mm index 06bca27e54..1cea72b7df 100644 --- a/iphone/Maps/Classes/MapViewController.mm +++ b/iphone/Maps/Classes/MapViewController.mm @@ -24,13 +24,16 @@ #include "../../../anim/controller.hpp" #include "../../../gui/controller.hpp" -#include "../../../platform/platform.hpp" #include "../Statistics/Statistics.h" + +#include "../../../map/country_status_display.hpp" #include "../../../map/dialog_settings.hpp" #include "../../../map/user_mark.hpp" -#include "../../../map/country_status_display.hpp" -#include "../../../platform/settings.hpp" + #include "../../../platform/file_logging.hpp" +#include "../../../platform/platform.hpp" +#include "../../../platform/settings.hpp" + #define ALERT_VIEW_FACEBOOK 1 #define ALERT_VIEW_APPSTORE 2 @@ -134,7 +137,7 @@ [m_predictor reset:info]; Framework & frm = GetFramework(); frm.OnLocationUpdate(info); - LogMemoryInfo(); + LOG_MEMORY_INFO(); [self showPopover]; [self updateRoutingInfo]; diff --git a/iphone/Maps/main.mm b/iphone/Maps/main.mm index 779d1979d4..1322db166d 100644 --- a/iphone/Maps/main.mm +++ b/iphone/Maps/main.mm @@ -2,9 +2,9 @@ #import #import -#include "../../platform/settings.hpp" -#include "../../platform/platform.hpp" #include "../../platform/file_logging.hpp" +#include "../../platform/platform.hpp" +#include "../../platform/settings.hpp" /// Used to trick iOs and enable multithreading support with non-native pthreads. diff --git a/platform/file_logging.cpp b/platform/file_logging.cpp index edaef6a4af..4bc391bf41 100644 --- a/platform/file_logging.cpp +++ b/platform/file_logging.cpp @@ -1,38 +1,52 @@ #include "file_logging.hpp" -#include "../base/mutex.hpp" - -#include "../std/chrono.hpp" +#include "../std/mutex.hpp" #include "../coding/file_writer.hpp" #include "../platform/platform.hpp" +namespace +{ + tm * GetLocalTime() + { + time_t rawTime; + time(&rawTime); + tm * localTime = localtime(&rawTime); + assert(localTime); + return localTime; + } +} void LogMessageFile(my::LogLevel level, my::SrcPoint const & srcPoint, string const & msg) { - static threads::Mutex mutex; - - threads::MutexGuard guard(mutex); - UNUSED_VALUE(guard); - + static mutex mtx; static unique_ptr file; + string recordType; + switch (level) + { + case LINFO: recordType.assign("INFO "); break; + case LDEBUG: recordType.assign("DEBUG "); break; + case LWARNING: recordType.assign("WARN "); break; + case LERROR: recordType.assign("ERROR "); break; + case LCRITICAL: recordType.assign("FATAL "); break; + } + + lock_guard lock(mtx); + if (file == nullptr) { if (GetPlatform().WritableDir().empty()) return; - auto const curTime = system_clock::now(); - time_t const curCTime = system_clock::to_time_t(curTime); - tm * curTimeTM = localtime(&curCTime); - assert(curTimeTM != nullptr); + tm * curTimeTM = GetLocalTime(); stringstream fileName; fileName << "logging_" << curTimeTM->tm_year + 1900 << "_" << curTimeTM->tm_mon + 1 << "_" << curTimeTM->tm_mday << "_" - << curTimeTM->tm_hour << "_" << curTimeTM->tm_min << "_" << curTimeTM->tm_sec << ".txt"; + << curTimeTM->tm_hour << "_" << curTimeTM->tm_min << "_" << curTimeTM->tm_sec << ".log"; file.reset(new FileWriter(GetPlatform().WritablePathForFile(fileName.str()))); } - string srcString = DebugPrint(srcPoint) + " " + msg + "\n"; + string srcString = recordType + DebugPrint(srcPoint) + " " + msg + "\n"; file->Write(srcString.c_str(), srcString.size()); file->Flush(); @@ -40,20 +54,14 @@ void LogMessageFile(my::LogLevel level, my::SrcPoint const & srcPoint, string co void LogMemoryInfo() { -#ifdef DEBUG static unsigned long counter = 0; - const unsigned short writeLogEveryNthLocationUpdate = 3; - if (counter % writeLogEveryNthLocationUpdate == 0) + const unsigned short writeLogEveryNthCall = 3; + if (counter++ % writeLogEveryNthCall == 0) { - auto const curTime = system_clock::now(); - time_t const curCTime = system_clock::to_time_t(curTime); - tm * curTimeTM = localtime(&curCTime); - ASSERT(curTimeTM != nullptr, ()); - stringstream fileName; - fileName << " " << curTimeTM->tm_year + 1900 << "." << curTimeTM->tm_mon + 1 << "." << curTimeTM->tm_mday << " " + tm * curTimeTM = GetLocalTime(); + stringstream timeDate; + timeDate << " " << curTimeTM->tm_year + 1900 << "." << curTimeTM->tm_mon + 1 << "." << curTimeTM->tm_mday << " " << curTimeTM->tm_hour << ":" << curTimeTM->tm_min << ":" << curTimeTM->tm_sec << " "; - LOG(LDEBUG, (fileName.str(), GetPlatform().GetMemoryInfo())); + LOG(LINFO, (timeDate.str(), GetPlatform().GetMemoryInfo())); } - ++counter; -#endif } diff --git a/platform/file_logging.hpp b/platform/file_logging.hpp index 7887a171e8..893342b4ee 100644 --- a/platform/file_logging.hpp +++ b/platform/file_logging.hpp @@ -2,9 +2,26 @@ #include "../base/logging.hpp" + +// @todo this functionality is not located in logging.hpp since file_logging uses FileWriter which depends on coding lib. +// At the same time loging is located in base and I do not want base depens on several more libs. +// Probably it's a good idea to move all logging functionality (logging, file_logging, mem_logging and so on) +// to a special subsystem which depends on base and coding. + // If you uncomment the line bellow the application log will be written to a file. -// You'll fild the file in MapsWithMe directory on Android platform and in Documents on iOS. +// You'll find a log file (logging_