Logging-related changes.
authormikhail.naganov@gmail.com <mikhail.naganov@gmail.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 1 Mar 2010 10:41:34 +0000 (10:41 +0000)
committermikhail.naganov@gmail.com <mikhail.naganov@gmail.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 1 Mar 2010 10:41:34 +0000 (10:41 +0000)
 - when logging 'open-tag' / 'close-tag' events, don't depend on
   FLAG_log (as it may be not enabled, e.g. in Chromium);

 - PauseProfiler / ResumeProfiler were supposing that they
   use 'is_logging_' var exclusively, thus preventing any
   other logging that may be turned on for diagnostic purposes.

Review URL: http://codereview.chromium.org/661246

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3986 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/log.cc
src/log.h
test/cctest/test-log.cc

index a3fef7310bfc027b2fbe6527e8028fc64f5e6229..9d0b35246b2f78611ae5064444cf7430b7b85a5c 100644 (file)
@@ -329,7 +329,7 @@ VMState Logger::bottom_state_(EXTERNAL);
 SlidingStateWindow* Logger::sliding_state_window_ = NULL;
 const char** Logger::log_events_ = NULL;
 CompressionHelper* Logger::compression_helper_ = NULL;
-bool Logger::is_logging_ = false;
+int Logger::logging_nesting_ = 0;
 int Logger::cpu_profiler_nesting_ = 0;
 int Logger::heap_profiler_nesting_ = 0;
 
@@ -389,12 +389,19 @@ void Logger::UncheckedStringEvent(const char* name, const char* value) {
 
 void Logger::IntEvent(const char* name, int value) {
 #ifdef ENABLE_LOGGING_AND_PROFILING
-  if (!Log::IsEnabled() || !FLAG_log) return;
+  if (FLAG_log) UncheckedIntEvent(name, value);
+#endif
+}
+
+
+#ifdef ENABLE_LOGGING_AND_PROFILING
+void Logger::UncheckedIntEvent(const char* name, int value) {
+  if (!Log::IsEnabled()) return;
   LogMessageBuilder msg;
   msg.Append("%s,%d\n", name, value);
   msg.WriteToLogFile();
-#endif
 }
+#endif
 
 
 void Logger::HandleEvent(const char* name, Object** location) {
@@ -1169,19 +1176,18 @@ void Logger::PauseProfiler(int flags, int tag) {
         // Must be the same message as Log::kDynamicBufferSeal.
         LOG(UncheckedStringEvent("profiler", "pause"));
       }
+      --logging_nesting_;
     }
   }
   if (flags &
       (PROFILER_MODULE_HEAP_STATS | PROFILER_MODULE_JS_CONSTRUCTORS)) {
     if (--heap_profiler_nesting_ == 0) {
       FLAG_log_gc = false;
+      --logging_nesting_;
     }
   }
   if (tag != 0) {
-    IntEvent("close-tag", tag);
-  }
-  if (GetActiveProfilerModules() == PROFILER_MODULE_NONE) {
-    is_logging_ = false;
+    UncheckedIntEvent("close-tag", tag);
   }
 }
 
@@ -1189,11 +1195,11 @@ void Logger::PauseProfiler(int flags, int tag) {
 void Logger::ResumeProfiler(int flags, int tag) {
   if (!Log::IsEnabled()) return;
   if (tag != 0) {
-    IntEvent("open-tag", tag);
+    UncheckedIntEvent("open-tag", tag);
   }
   if (flags & PROFILER_MODULE_CPU) {
     if (cpu_profiler_nesting_++ == 0) {
-      is_logging_ = true;
+      ++logging_nesting_;
       if (FLAG_prof_lazy) {
         profiler_->Engage();
         LOG(UncheckedStringEvent("profiler", "resume"));
@@ -1209,7 +1215,7 @@ void Logger::ResumeProfiler(int flags, int tag) {
   if (flags &
       (PROFILER_MODULE_HEAP_STATS | PROFILER_MODULE_JS_CONSTRUCTORS)) {
     if (heap_profiler_nesting_++ == 0) {
-      is_logging_ = true;
+      ++logging_nesting_;
       FLAG_log_gc = true;
     }
   }
@@ -1482,14 +1488,16 @@ bool Logger::Setup() {
     compression_helper_ = new CompressionHelper(kCompressionWindowSize);
   }
 
-  is_logging_ = start_logging;
+  if (start_logging) {
+    logging_nesting_ = 1;
+  }
 
   if (FLAG_prof) {
     profiler_ = new Profiler();
     if (!FLAG_prof_auto) {
       profiler_->pause();
     } else {
-      is_logging_ = true;
+      logging_nesting_ = 1;
     }
     if (!FLAG_prof_lazy) {
       profiler_->Engage();
index eb8369cf00bd8e23bc7f0a1df2cf29b765464e70..613a1e26cfb58bf44dc5e809e54330441c2412b4 100644 (file)
--- a/src/log.h
+++ b/src/log.h
@@ -265,7 +265,7 @@ class Logger {
   }
 
   static bool is_logging() {
-    return is_logging_;
+    return logging_nesting_ > 0;
   }
 
   // Pause/Resume collection of profiling data.
@@ -330,6 +330,9 @@ class Logger {
   // Logs a StringEvent regardless of whether FLAG_log is true.
   static void UncheckedStringEvent(const char* name, const char* value);
 
+  // Logs an IntEvent regardless of whether FLAG_log is true.
+  static void UncheckedIntEvent(const char* name, int value);
+
   // Stops logging and profiling in case of insufficient resources.
   static void StopLoggingAndProfiling();
 
@@ -372,7 +375,7 @@ class Logger {
 
   friend class LoggerTestHelper;
 
-  static bool is_logging_;
+  static int logging_nesting_;
   static int cpu_profiler_nesting_;
   static int heap_profiler_nesting_;
 #else
index 9853af32444fa4c40365b4c7110ced67dc3bcd46..6b3639c49605e25a07f65f9ffcebc5eeb0d8a438 100644 (file)
@@ -174,12 +174,11 @@ namespace {
 
 class ScopedLoggerInitializer {
  public:
-  explicit ScopedLoggerInitializer(bool log, bool prof_lazy)
-      : saved_log_(i::FLAG_log),
-        saved_prof_lazy_(i::FLAG_prof_lazy),
+  explicit ScopedLoggerInitializer(bool prof_lazy)
+      : saved_prof_lazy_(i::FLAG_prof_lazy),
         saved_prof_(i::FLAG_prof),
         saved_prof_auto_(i::FLAG_prof_auto),
-        trick_to_run_init_flags_(init_flags_(log, prof_lazy)),
+        trick_to_run_init_flags_(init_flags_(prof_lazy)),
         need_to_set_up_logger_(i::V8::IsRunning()),
         scope_(),
         env_(v8::Context::New()) {
@@ -193,14 +192,12 @@ class ScopedLoggerInitializer {
     i::FLAG_prof_lazy = saved_prof_lazy_;
     i::FLAG_prof = saved_prof_;
     i::FLAG_prof_auto = saved_prof_auto_;
-    i::FLAG_log = saved_log_;
   }
 
   v8::Handle<v8::Context>& env() { return env_; }
 
  private:
-  static bool init_flags_(bool log, bool prof_lazy) {
-    i::FLAG_log = log;
+  static bool init_flags_(bool prof_lazy) {
     i::FLAG_prof = true;
     i::FLAG_prof_lazy = prof_lazy;
     i::FLAG_prof_auto = false;
@@ -208,7 +205,6 @@ class ScopedLoggerInitializer {
     return prof_lazy;
   }
 
-  const bool saved_log_;
   const bool saved_prof_lazy_;
   const bool saved_prof_;
   const bool saved_prof_auto_;
@@ -320,7 +316,7 @@ static void CheckThatProfilerWorks(LogBufferMatcher* matcher) {
 
 
 TEST(ProfLazyMode) {
-  ScopedLoggerInitializer initialize_logger(false, true);
+  ScopedLoggerInitializer initialize_logger(true);
 
   // No sampling should happen prior to resuming profiler.
   CHECK(!LoggerTestHelper::IsSamplerActive());
@@ -540,7 +536,7 @@ static v8::Handle<v8::Value> ObjMethod1(const v8::Arguments& args) {
 }
 
 TEST(LogCallbacks) {
-  ScopedLoggerInitializer initialize_logger(false, false);
+  ScopedLoggerInitializer initialize_logger(false);
   LogBufferMatcher matcher;
 
   v8::Persistent<v8::FunctionTemplate> obj =
@@ -590,7 +586,7 @@ static v8::Handle<v8::Value> Prop2Getter(v8::Local<v8::String> property,
 }
 
 TEST(LogAccessorCallbacks) {
-  ScopedLoggerInitializer initialize_logger(false, false);
+  ScopedLoggerInitializer initialize_logger(false);
   LogBufferMatcher matcher;
 
   v8::Persistent<v8::FunctionTemplate> obj =
@@ -625,7 +621,7 @@ TEST(LogAccessorCallbacks) {
 
 
 TEST(LogTags) {
-  ScopedLoggerInitializer initialize_logger(true, false);
+  ScopedLoggerInitializer initialize_logger(false);
   LogBufferMatcher matcher;
 
   const char* open_tag = "open-tag,";
@@ -710,6 +706,35 @@ TEST(LogTags) {
 }
 
 
+TEST(IsLoggingPreserved) {
+  ScopedLoggerInitializer initialize_logger(false);
+
+  CHECK(Logger::is_logging());
+  Logger::ResumeProfiler(v8::PROFILER_MODULE_CPU, 1);
+  CHECK(Logger::is_logging());
+  Logger::PauseProfiler(v8::PROFILER_MODULE_CPU, 1);
+  CHECK(Logger::is_logging());
+
+  CHECK(Logger::is_logging());
+  Logger::ResumeProfiler(
+      v8::PROFILER_MODULE_HEAP_STATS | v8::PROFILER_MODULE_JS_CONSTRUCTORS, 1);
+  CHECK(Logger::is_logging());
+  Logger::PauseProfiler(
+      v8::PROFILER_MODULE_HEAP_STATS | v8::PROFILER_MODULE_JS_CONSTRUCTORS, 1);
+  CHECK(Logger::is_logging());
+
+  CHECK(Logger::is_logging());
+  Logger::ResumeProfiler(
+      v8::PROFILER_MODULE_CPU |
+      v8::PROFILER_MODULE_HEAP_STATS | v8::PROFILER_MODULE_JS_CONSTRUCTORS, 1);
+  CHECK(Logger::is_logging());
+  Logger::PauseProfiler(
+      v8::PROFILER_MODULE_CPU |
+      v8::PROFILER_MODULE_HEAP_STATS | v8::PROFILER_MODULE_JS_CONSTRUCTORS, 1);
+  CHECK(Logger::is_logging());
+}
+
+
 static inline bool IsStringEqualTo(const char* r, const char* s) {
   return strncmp(r, s, strlen(r)) == 0;
 }