1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Helper functions for Service implementations.
16 :
17 : #ifndef SYZYGY_TRACE_COMMON_SERVICE_UTIL_H_
18 : #define SYZYGY_TRACE_COMMON_SERVICE_UTIL_H_
19 :
20 : #include <memory>
21 :
22 : #include "base/command_line.h"
23 : #include "base/strings/string_piece.h"
24 : #include "base/win/scoped_handle.h"
25 :
26 m : namespace trace {
27 m : namespace common {
28 :
29 : // Helper function to acquire a named mutex. Once acquired this mutex must be
30 : // freed using ::ReleaseMutex.
31 : // @param mutex_name The name of the mutex to acquire.
32 : // @param mutex will receive a handle to the named mutex.
33 : // @returns true on success, false otherwise. Logs verbosely on failure.
34 m : bool AcquireMutex(const base::StringPiece16& mutex_name,
35 m : base::win::ScopedHandle* mutex);
36 :
37 : // Helper function to initialize a named event. The event will automatically be
38 : // destroyed when the last handle to it disappears.
39 : // @param event_name The name of event to create.
40 : // @param handle Will receive a handle to the named event.
41 : // @returns true on success, false otherwise. Logs verbosely on failure.
42 m : bool InitEvent(const base::StringPiece16& event_name,
43 m : base::win::ScopedHandle* handle);
44 :
45 : // A helper to split a command line into two command lines. The split will
46 : // occur after the first non-switch parameter. The logger command line will
47 : // be populated by the switches and arguments up to and including the first
48 : // non-switch parameter. All remaining arguments and switches will be added
49 : // to the app command line. This function understands the "--" marker
50 : // which is used to allow switches to appear after the first non-switch
51 : // argument (otherwise base::CommandLine will sort the entire command line
52 : // before we get a chance to inspect it.).
53 m : bool SplitCommandLine(const base::CommandLine* orig_command_line,
54 m : base::CommandLine* logger_command_line,
55 m : std::unique_ptr<base::CommandLine>* app_command_line);
56 :
57 : // A helper class to manage a console handler for Control-C.
58 m : class ScopedConsoleCtrlHandler {
59 m : public:
60 m : ScopedConsoleCtrlHandler();
61 m : ~ScopedConsoleCtrlHandler();
62 m : bool Init(PHANDLER_ROUTINE handler);
63 :
64 m : protected:
65 m : PHANDLER_ROUTINE handler_;
66 m : };
67 :
68 m : } // namespace common
69 m : } // namespace trace
70 :
71 : #endif // SYZYGY_TRACE_COMMON_SERVICE_UTIL_H_
|