1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_TEST_WEBDRIVER_WEBDRIVER_LOGGING_H_ 6 #define CHROME_TEST_WEBDRIVER_WEBDRIVER_LOGGING_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/file_util.h" 13 #include "base/files/file_path.h" 14 #include "base/synchronization/lock.h" 15 #include "base/values.h" 16 17 namespace base { 18 class Time; 19 } 20 21 namespace webdriver { 22 23 // WebDriver logging levels. 24 enum LogLevel { 25 kOffLogLevel = 1200, 26 kSevereLogLevel = 1000, 27 kWarningLogLevel = 900, 28 kInfoLogLevel = 800, 29 kFineLogLevel = 500, 30 kFinerLogLevel = 400, 31 kAllLogLevel = -1000 32 }; 33 34 // |name| should be a webdriver log level, such as "INFO", "SEVERE", etc. 35 LogLevel LogLevelFromString(const std::string& name); 36 37 // Represents a type/source of a WebDriver log. 38 class LogType { 39 public: 40 enum Type { 41 kInvalid = -1, 42 kDriver, 43 kNum // must be correct 44 }; 45 46 static bool FromString(const std::string& name, LogType* log_type); 47 48 LogType(); 49 explicit LogType(Type type); 50 ~LogType(); 51 52 std::string ToString() const; 53 Type type() const; 54 55 private: 56 Type type_; 57 }; 58 59 class LogHandler { 60 public: 61 LogHandler(); 62 virtual ~LogHandler(); 63 64 virtual void Log(LogLevel level, const base::Time& time, 65 const std::string& message) = 0; 66 67 private: 68 DISALLOW_COPY_AND_ASSIGN(LogHandler); 69 }; 70 71 class FileLog : public LogHandler { 72 public: 73 static void SetGlobalLog(FileLog* log); 74 static FileLog* Get(); 75 76 // Creates a log file with the given name in the current directory. 77 // If the current directory is not writable, the system's temp directory 78 // is used. 79 static FileLog* CreateFileLog(const base::FilePath::StringType& log_name, 80 LogLevel level); 81 82 // Creates a log file at the given path. 83 FileLog(const base::FilePath& path, LogLevel level); 84 85 virtual ~FileLog(); 86 87 virtual void Log(LogLevel level, const base::Time& time, 88 const std::string& message) OVERRIDE; 89 90 bool GetLogContents(std::string* contents) const; 91 92 // Returns whether the log refers to an open file. 93 bool IsOpen() const; 94 95 void set_min_log_level(LogLevel level); 96 97 // Returns the path of the log file. The file is not guaranteed to exist. 98 const base::FilePath& path() const; 99 100 private: 101 static FileLog* singleton_; 102 103 base::FilePath path_; 104 file_util::ScopedFILE file_; 105 base::Lock lock_; 106 107 LogLevel min_log_level_; 108 109 DISALLOW_COPY_AND_ASSIGN(FileLog); 110 }; 111 112 class InMemoryLog : public LogHandler { 113 public: 114 InMemoryLog(); 115 virtual ~InMemoryLog(); 116 117 virtual void Log(LogLevel level, const base::Time& time, 118 const std::string& message) OVERRIDE; 119 120 const base::ListValue* entries_list() const; 121 122 private: 123 base::ListValue entries_list_; 124 base::Lock entries_lock_; 125 126 DISALLOW_COPY_AND_ASSIGN(InMemoryLog); 127 }; 128 129 // Forwards logging messages to added logs. 130 class Logger { 131 public: 132 Logger(); 133 explicit Logger(LogLevel level); 134 ~Logger(); 135 136 void Log(LogLevel level, const std::string& message) const; 137 void AddHandler(LogHandler* log_handler); 138 139 void set_min_log_level(LogLevel level); 140 141 private: 142 std::vector<LogHandler*> handlers_; 143 LogLevel min_log_level_; 144 }; 145 146 // Initializes logging for WebDriver. All logging below the given level 147 // will be discarded in the global file log. The file log will use the given 148 // log path. If the specified log path is empty, the log will write to 149 // 'chromedriver.log' in the current working directory, if writeable, or the 150 // system temp directory. Returns true on success. 151 bool InitWebDriverLogging(const base::FilePath& log_path, 152 LogLevel min_log_level); 153 154 } // namespace webdriver 155 156 #endif // CHROME_TEST_WEBDRIVER_WEBDRIVER_LOGGING_H_ 157