chromium/components/device_event_log/device_event_log.h

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_
#define COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_

#include <stddef.h>

#include <cstring>
#include <sstream>

#include "base/check.h"
#include "base/logging.h"
#include "base/timer/elapsed_timer.h"
#include "build/build_config.h"
#include "components/device_event_log/device_event_log_export.h"

// These macros can be used to log device related events.
//
// NOTE: If these macros are called from a thread other than the thread that
// device_event_log::Initialize() was called from (i.e. the UI thread), a task
// will be posted to the UI thread to log the event.
//
// The following values should be used for |level| in these macros:
//  ERROR Unexpected events, or device level failures. Use sparingly.
//  USER  Events initiated directly by a user (or Chrome) action.
//  EVENT Default event type.
//  DEBUG Debugging details that are usually not interesting.
//
// Examples:
//  NET_LOG(EVENT) << "NetworkState Changed " << name << ": " << state;
//  POWER_LOG(USER) << "Suspend requested";
//
// See also the README.md in this directory.

#define NET_LOG(level)
#define POWER_LOG(level)
#define LOGIN_LOG(level)
#define BLUETOOTH_LOG(level)
#define USB_LOG(level)
#define USB_PLOG(level)
#define HID_LOG(level)
#define HID_PLOG(level)
#define MEMORY_LOG(level)
#define PRINTER_LOG(level)
#define SERIAL_LOG(level)
#define SERIAL_PLOG(level)
#define CAMERA_LOG(level)
#define GEOLOCATION_LOG(level)
#define EXTENSIONS_LOG(level)
#define DISPLAY_LOG(level)
#define FIRMWARE_LOG(level)

#if BUILDFLAG(IS_ANDROID) && defined(OFFICIAL_BUILD)
// FIDO_LOG is discarded for release Android builds in order to reduce binary
// size.
#define FIDO_LOG
#else
#define FIDO_LOG(level)
#endif

// Generally prefer the above macros unless |type| or |level| is not constant.

#define DEVICE_LOG(type, level)
#define DEVICE_PLOG(type, level)

// Declare {Type_LOG_IF_SLOW() at the top of a method to log slow methods
// where "slow" is defined by kSlowMethodThresholdMs in the .cc file.
#define SCOPED_NET_LOG_IF_SLOW()

// Generally prefer the above macros unless |type| is not constant.

#define SCOPED_DEVICE_LOG_IF_SLOW(type)

namespace device_event_log {

// Used to specify the type of event. Consider updating chrome://device-log
// when adding new types (see device_log_ui.cc).
enum LogType {};

// Used to specify the detail level for logging. In GetAsString, used to
// specify the maximum detail level (i.e. EVENT will include USER and ERROR).
// See top-level comment for guidelines for each type.
enum LogLevel {};

// Used to specify which order to output event entries in GetAsString.
enum StringOrder {};

// Initializes / shuts down device event logging. If |max_entries| = 0 the
// default value will be used.
void DEVICE_EVENT_LOG_EXPORT Initialize(size_t max_entries);
bool DEVICE_EVENT_LOG_EXPORT IsInitialized();
void DEVICE_EVENT_LOG_EXPORT Shutdown();

// If the global instance is initialized, adds an entry to it. Regardless of
// whether the global instance was intitialzed, this logs the event to
// LOG(ERROR) if |type| = ERROR or VLOG(1) otherwise.
void DEVICE_EVENT_LOG_EXPORT AddEntry(const char* file,
                                      int line,
                                      LogType type,
                                      LogLevel level,
                                      const std::string& event);

// For backwards compatibility with network_event_log. Combines |event| and
// |description| and calls AddEntry().
void DEVICE_EVENT_LOG_EXPORT
AddEntryWithDescription(const char* file,
                        int line,
                        LogType type,
                        LogLevel level,
                        const std::string& event,
                        const std::string& description);

// Outputs the log to a formatted string.
// |order| determines which order to output the events.
// |format| is a comma-separated string that determines which elements to show.
//  e.g. "time,desc". Note: order of the strings does not affect the output.
//  "time" - Include a timestamp.
//  "file" - Include file and line number.
//  "type" - Include the event type.
//  "json" - Return JSON format dictionaries containing entries for timestamp,
//           level, type, file, and event.
// |types| lists the types included in the output. Prepend "non-" to disclude
//  a type. e.g. "network,login" or "non-network". Use an empty string for
//  all types.
// |max_level| determines the maximum log level to be included in the output.
// |max_events| limits how many events are output if > 0, otherwise all events
//  are included.
std::string DEVICE_EVENT_LOG_EXPORT GetAsString(StringOrder order,
                                                const std::string& format,
                                                const std::string& types,
                                                LogLevel max_level,
                                                size_t max_events);

// Clear all entries from the device event log.
void DEVICE_EVENT_LOG_EXPORT ClearAll();

// Clear entries from the device event log between the given times.
void DEVICE_EVENT_LOG_EXPORT Clear(const base::Time& begin,
                                   const base::Time& end);

DEVICE_EVENT_LOG_EXPORT extern const LogLevel kDefaultLogLevel;

int DEVICE_EVENT_LOG_EXPORT GetCountByLevelForTesting(LogLevel level);

namespace internal {

// Implementation class for DEVICE_LOG macros. Provides a stream for creating
// a log string and adds the event using device_event_log::AddEntry on
// destruction.
class DEVICE_EVENT_LOG_EXPORT DeviceEventLogInstance {};

// Implementation class for DEVICE_PLOG macros. Provides a stream for creating
// a log string and adds the event, including system error code, using
// device_event_log::AddEntry on destruction.
class DEVICE_EVENT_LOG_EXPORT DeviceEventSystemErrorLogInstance {};

// Implementation class for SCOPED_LOG_IF_SLOW macros. Tests the elapsed time on
// destruction and adds a Debug or Error log entry if it exceeds the
// corresponding expected maximum elapsed time.
class DEVICE_EVENT_LOG_EXPORT ScopedDeviceLogIfSlow {};

}  // namespace internal

}  // namespace device_event_log

#endif  // COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_H_