1 // Copyright 2015 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.
5 #ifndef CHROMECAST_CRASH_LINUX_MINIDUMP_WRITER_H_
6 #define CHROMECAST_CRASH_LINUX_MINIDUMP_WRITER_H_
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/macros.h"
11 #include "base/time/time.h"
12 #include "chromecast/crash/linux/minidump_params.h"
13 #include "chromecast/crash/linux/synchronized_minidump_manager.h"
15 namespace chromecast
{
17 class MinidumpGenerator
;
19 // Class for writing a minidump with synchronized access to the minidumps
21 class MinidumpWriter
: public SynchronizedMinidumpManager
{
23 typedef base::Callback
<int(const std::string
&)> DumpStateCallback
;
25 // Constructs a writer for a minidump. If |minidump_filename| is absolute, it
26 // must be a path to a file in the |dump_path_| directory. Otherwise, it
27 // should be a filename only, in which case, |minidump_generator| creates
28 // a minidump at $HOME/minidumps/|minidump_filename|. |params| describes the
29 // minidump metadata. |dump_state_cb| is Run() to generate a log dump. Please
30 // see the comments on |dump_state_cb_| below for details about this
32 // This does not take ownership of |minidump_generator|.
33 MinidumpWriter(MinidumpGenerator
* minidump_generator
,
34 const std::string
& minidump_filename
,
35 const MinidumpParams
& params
,
36 const base::Callback
<int(const std::string
&)>& dump_state_cb
);
38 // Like the constructor above, but the default implementation of
39 // |dump_state_cb_| is used inside DoWork().
40 MinidumpWriter(MinidumpGenerator
* minidump_generator
,
41 const std::string
& minidump_filename
,
42 const MinidumpParams
& params
);
44 ~MinidumpWriter() override
;
46 // Acquires exclusive access to the minidumps directory and generates a
47 // minidump and a log to be uploaded later. Returns 0 if successful, -1
49 int Write() { return AcquireLockAndDoWork(); }
51 int max_dumps() const { return max_dumps_
; }
52 int max_recent_dumps() const { return max_recent_dumps_
; }
53 const base::TimeDelta
& dump_interval() const { return dump_interval_
; };
56 // MinidumpManager implementation:
57 int DoWork() override
;
60 // Returns true if we can write another dump, false otherwise. We can write
61 // another dump if the number of minidumps is strictly less than |max_dumps_|
62 // and the number of minidumps which occurred within the last |dump_interval_|
63 // is strictly less than |max_recent_dumps_|.
66 MinidumpGenerator
* const minidump_generator_
;
67 base::FilePath minidump_path_
;
68 const MinidumpParams params_
;
71 const base::TimeDelta dump_interval_
;
72 const int max_recent_dumps_
;
74 // This callback is Run() to dump a log to |minidump_path_|.txt.gz, taking
75 // |minidump_path_| as a parameter. It returns 0 on success, and a negative
76 // integer otherwise. If a callback is not passed in the constructor, the
77 // default implemementaion is used.
78 DumpStateCallback dump_state_cb_
;
80 DISALLOW_COPY_AND_ASSIGN(MinidumpWriter
);
83 } // namespace chromecast
85 #endif // CHROMECAST_CRASH_LINUX_MINIDUMP_WRITER_H_