1 : // Copyright 2012 Google Inc.
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 : // Defines the template member function of the common::Application template
16 : // class.
17 : //
18 : // This is not meant to be included directly.
19 :
20 : #ifndef SYZYGY_COMMON_APPLICATION_IMPL_H_
21 : #define SYZYGY_COMMON_APPLICATION_IMPL_H_
22 :
23 : #include "syzygy/common/syzygy_version.h"
24 :
25 : namespace common {
26 :
27 : namespace internal {
28 :
29 : // A helper class to initialize and uninitialize COM within a context.
30 : // TODO(rogerm): Move to com_utils library, either in sawbuck or create a
31 : // new one for syzygy.
32 : class ScopedComInitializer {
33 : public:
34 : // Initialize COM in this context.
35 E : ScopedComInitializer() : hresult_(::CoInitialize(NULL)) {
36 E : if (!succeeded())
37 i : LOG(ERROR) << "CoInitialize() failed: " << com::LogHr(hresult()) << ".";
38 E : }
39 :
40 : // Deinitialized COM if initialization was successful.
41 E : ~ScopedComInitializer() {
42 E : if (succeeded())
43 E : ::CoUninitialize();
44 E : }
45 :
46 : // Get the status returned by the initialization.
47 E : HRESULT hresult() const { return hresult_; }
48 :
49 : // True if the initialization succeeded.
50 E : bool succeeded() const { return SUCCEEDED(hresult()); }
51 :
52 : private:
53 : // The status returned by the initialization.
54 : const HRESULT hresult_;
55 : };
56 :
57 : } // namespace common::internal
58 :
59 : template <typename Impl, AppLoggingFlag kInitLogging>
60 : Application<Impl, kInitLogging>::Application()
61 E : : command_line_(CommandLine::ForCurrentProcess()) {
62 E : }
63 :
64 : template <typename Impl, AppLoggingFlag kInitLogging>
65 E : int Application<Impl, kInitLogging>::Run() {
66 : // If we've been asked for our version, spit it out and quit.
67 E : if (command_line_->HasSwitch("version")) {
68 i : ::fprintf(out(), "%s\n", kSyzygyVersion.GetVersionString().c_str());
69 i : return 0;
70 : }
71 :
72 E : if (!InitializeLogging())
73 i : return 1;
74 :
75 E : LOG(INFO) << "Syzygy " << implementation_.name()
76 : << " Version " << kSyzygyVersion.GetVersionString() << ".";
77 E : LOG(INFO) << "Copyright (c) Google Inc. All rights reserved.";
78 :
79 E : internal::ScopedComInitializer com_initializer;
80 E : if (!com_initializer.succeeded())
81 i : return 1;
82 :
83 E : if (!implementation_.ParseCommandLine(command_line_))
84 E : return 1;
85 :
86 E : if (!implementation_.SetUp())
87 E : return 1;
88 :
89 E : int result = implementation_.Run();
90 :
91 E : implementation_.TearDown();
92 :
93 E : return result;
94 E : }
95 :
96 : template <typename Impl, AppLoggingFlag kInitLogging>
97 E : bool Application<Impl, kInitLogging>::InitializeLogging() {
98 : if ((kInitLogging == INIT_LOGGING_YES) &&
99 : !logging::InitLogging(
100 : L"",
101 : logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
102 : logging::DONT_LOCK_LOG_FILE,
103 : logging::APPEND_TO_OLD_LOG_FILE,
104 E : logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) {
105 i : return false;
106 : }
107 :
108 E : if (command_line_->HasSwitch("verbose")) {
109 E : std::string value_str(command_line_->GetSwitchValueASCII("verbose"));
110 E : TrimWhitespace(value_str, TRIM_ALL, &value_str);
111 E : int value = 1;
112 E : if (!base::StringToInt(value_str, &value))
113 i : value = 1;
114 E : logging::SetMinLogLevel(-::abs(value));
115 E : }
116 :
117 E : return true;
118 E : }
119 :
120 : } // namespace common
121 :
122 : #endif // SYZYGY_COMMON_APPLICATION_IMPL_H_
|