-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
198 lines (178 loc) · 7.81 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* Copyright 2015-2016 Pascal COMBES <pascom@orange.fr>
*
* This file is part of GlobalHotKeys.
*
* GlobalHotKeys is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GlobalHotKeys is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GlobalHotKeys. If not, see <http://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Repository.Hierarchy;
namespace GlobalHotKeys
{
/// <summary>
/// Main class of GlobalHotKeys
/// </summary>
/// <para>Handles argument parsing and initialisation of the program.</para>
/// <para>
/// GlobalHotKeys invocation:
/// <code>GlobalHotKeys.exe [(--quiet|--debug)] [configPath] [processesPath]</code>
/// </para>
/// <list>
/// <item><term><c>--quiet</c>: </term><description>Quietly run (no file logging)</description></item>
/// <item><term><c>--debug</c>: </term><description>Run ind debug mode (log level set to debug)</description></item>
/// <item><term>configPath: </term><description>The path to the file where the shortcuts are defined.</description></item>
/// <item><term>processesPath: </term><description>The path to the file where process info is listed. Only these processes can be started by GlobalHotKeys. This file should not be writable by normal users.</description></item>
/// </list>
/// <para>
/// Actually, only plain text configuration is supported by GlobalHotKeys.
/// To see the format of such files <see cref="PlainTextConfig"/> and look at the example files provided in the project root directory.
/// I have in project to develop registry based configuration as well as XML configuration files.
/// </para>
class Program
{
/// <summary>
/// Logger for GlobalHokKeys.
/// </summary>
/// <remarks>See Apache Log4net documentation for the logging interface.</remarks>
static private ILog log;
/// <summary>
/// Effectively start the program
/// </summary>
/// <para>
/// This function handles the command line arguments containing the config path and the processes path.
/// It then initialies the component with the suitable configuration.
/// </para>
/// <param name="argStart">Index pointing after the last argument read by <see cref="Main"/>.</param>
/// <param name="args">Command line arguments.</param>
static void Start(int argStart, string[] args)
{
string configPath;
string processesPath;
if (args.Length > argStart)
configPath = args[argStart];
else
configPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Pascom\GlobalHotKeys\globalhotkeys.conf";
if (args.Length > argStart + 1)
processesPath = args[argStart + 1];
else
processesPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Pascom\GlobalHotKeys\globalhotkeys.processes.conf";
log.Info("Starting GlobalHotKeys with configuration file :" + configPath);
log.Info("Starting GlobalHotKeys with process list file :" + processesPath);
Windows.Manager.ProcessessList = new Windows.PlainTextConfig(processesPath);
Shortcuts.Handler app = Shortcuts.Handler.getInstance(new PlainTextConfig(configPath));
app.exec();
}
/// <summary>
/// Set log level to Debug
/// </summary>
/// <para>
/// This function handles the command line argument <c>--debug</c>.
/// It sets the log level of all loggers to debug.
/// </para>
private static void setDebug()
{
Hierarchy repo = LogManager.GetRepository() as Hierarchy;
if (repo == null)
return;
repo.Root.Level = Level.Debug;
foreach (ILogger iLogger in repo.GetCurrentLoggers()) {
Logger logger = iLogger as Logger;
if (logger != null)
logger.Level = Level.Debug;
}
}
/// <summary>
/// Removes all appenders but Windows Event Log
/// </summary>
/// <param name="logger">The logger from which appenders will be removed</param>
/// <seealso cref="setQuiet"/>
private static void removeFileAppenders(Logger logger)
{
if (logger == null)
return;
AppenderCollection appenders = logger.Appenders;
for (int i = 0; i < appenders.Count; i++)
if (appenders[i].Name != "WindowsEventLog")
logger.RemoveAppender(appenders[i]);
}
/// <summary>
/// Removes file logging
/// </summary>
/// <para>
/// This function handles the command line argument <c>--quiet</c>.
/// It removes all file logging.
/// </para>
private static void setQuiet()
{
Hierarchy repo = LogManager.GetRepository() as Hierarchy;
if (repo == null)
return;
removeFileAppenders(repo.Root);
foreach (ILogger iLogger in LogManager.GetRepository().GetCurrentLoggers())
removeFileAppenders(iLogger as Logger);
}
/// <summary>
/// Program main function
/// </summary>
/// <para>
/// Handles the processiong of <c>--debug</c> and <c>--quiet</c> arguments.
/// Also wrap the core of the program in an exception handler.
/// </para>
/// <param name="args">Command line arguments.</param>
/// <seealso cref="Start"/>
static void Main(string[] args)
{
int processed = 0;
bool debuging = false;
bool quiet = false;
while ((processed < args.Length) && args[processed].StartsWith("--")) {
switch (args[processed]) {
case "--quiet":
quiet = true;
break;
case "--debug":
debuging = true;
break;
default:
log.Warn("Unknown argument: " + args[processed]);
break;
}
processed++;
}
#if DEBUG
log4net.GlobalContext.Properties["LogFilePath"] = System.IO.Path.GetTempPath() + @"\Pascom\GlobalHotKeys\Debug";
debuging = true;
#else
log4net.GlobalContext.Properties["LogFilePath"] = System.IO.Path.GetTempPath() + @"\Pascom\GlobalHotKeys\Release";
#endif
log4net.Config.XmlConfigurator.Configure();
if (quiet)
setQuiet();
if (debuging)
setDebug();
log = LogManager.GetLogger(typeof(Program));
try {
Start(processed, args);
} catch (Exception e) {
log.Fatal("Unhandled exception caught by main handler", e);
}
}
}
}