blob: 5222eedab330a83eb68ddcaac2547d0348bd436b [file] [log] [blame]
Elly Jones03cd6d72012-06-11 13:04:28 -04001// Copyright (c) 2012 The Chromium OS 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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#ifndef DEBUGD_SRC_LOG_TOOL_H_
6#define DEBUGD_SRC_LOG_TOOL_H_
Elly Jones03cd6d72012-06-11 13:04:28 -04007
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -07008#include <map>
Ben Chan5facf4a2014-07-23 16:36:54 -07009#include <string>
Elly Jones03cd6d72012-06-11 13:04:28 -040010
Eric Caruso0b241882018-04-04 13:43:46 -070011#include <base/files/scoped_file.h>
Ben Chan657bed82014-09-02 20:40:51 -070012#include <base/macros.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070013#include <base/memory/ref_counted.h>
14#include <dbus/bus.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040015
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080016#include "debugd/src/anonymizer_tool.h"
17
Elly Jones03cd6d72012-06-11 13:04:28 -040018namespace debugd {
19
20class LogTool {
21 public:
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070022 // The encoding for a particular log.
23 enum class Encoding {
24 // Tries to see if the log output is valid UTF-8. Outputs it as-is if it is,
25 // or base64-encodes it otherwise.
26 kAutodetect,
27
28 // Replaces any characters that are not valid UTF-8 encoded with the
29 // replacement character.
30 kUtf8,
31
32 // base64-encodes the output.
33 kBinary
34 };
35
Eric Carusof9091f82017-04-28 14:18:59 -070036 explicit LogTool(scoped_refptr<dbus::Bus> bus) : bus_(bus) {}
Ben Chan78f89532014-08-29 09:35:09 -070037 ~LogTool() = default;
Elly Jones03cd6d72012-06-11 13:04:28 -040038
Ben Chan81905fb2017-02-08 22:03:11 -080039 using LogMap = std::map<std::string, std::string>;
Elly Jones533c7c42012-08-10 15:07:05 -040040
Eric Carusoc93a15c2017-04-24 16:15:12 -070041 std::string GetLog(const std::string& name);
Eric Carusof9091f82017-04-28 14:18:59 -070042 LogMap GetAllLogs();
Brian Norrisca4fc042018-04-03 00:24:26 -070043 LogMap GetAllDebugLogs();
Eric Carusof9091f82017-04-28 14:18:59 -070044 LogMap GetFeedbackLogs();
Eric Caruso0b241882018-04-04 13:43:46 -070045 void GetBigFeedbackLogs(const base::ScopedFD& fd);
Eric Carusoc93a15c2017-04-24 16:15:12 -070046 LogMap GetUserLogFiles();
Darin Petkovce9b3a12013-01-10 16:38:54 +010047
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070048 // Returns a representation of |value| that is valid UTF-8 encoded. The value
49 // of |source_encoding| determines whether it will use Unicode U+FFFD
50 // REPLACEMENT CHARACTER or base64-encode the whole string in case there are
51 // invalid characters.
52 static std::string EnsureUTF8String(const std::string& value,
53 Encoding source_encoding);
54
Elly Jones03cd6d72012-06-11 13:04:28 -040055 private:
Darin Petkovce9b3a12013-01-10 16:38:54 +010056 friend class LogToolTest;
57
58 void AnonymizeLogMap(LogMap* log_map);
Eric Carusof9091f82017-04-28 14:18:59 -070059 void CreateConnectivityReport();
60
61 scoped_refptr<dbus::Bus> bus_;
Darin Petkovce9b3a12013-01-10 16:38:54 +010062
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080063 AnonymizerTool anonymizer_;
64
Elly Jones03cd6d72012-06-11 13:04:28 -040065 DISALLOW_COPY_AND_ASSIGN(LogTool);
66};
67
Ben Chana0011d82014-05-13 00:19:29 -070068} // namespace debugd
Elly Jones03cd6d72012-06-11 13:04:28 -040069
Alex Vakulenko262be3f2014-07-30 15:25:50 -070070#endif // DEBUGD_SRC_LOG_TOOL_H_