Skip to content

Commit e565660

Browse files
authored
Log file (#588)
Add file logging option
1 parent 4ff9af1 commit e565660

File tree

32 files changed

+222
-55
lines changed

32 files changed

+222
-55
lines changed

doc/usage.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,10 @@ CodeCompass_webserver \
285285

286286
The server will be available in a browser on
287287
[`http://localhost:6251`](http://localhost:6251).
288+
289+
### Logging
290+
291+
In both the parser and the webserver it is possible to write the logs to a given directory.
292+
This feature can be enabled by passing the `--logtarget` command line option with the full
293+
path to the directory to be used for storing the log files.
294+
If this argument is not specified, the logs will be written to the terminal only.

parser/src/parser.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ po::options_description commandLineArguments()
7070
po::value<trivial::severity_level>()->default_value(trivial::info),
7171
"Logging legel of the parser. Possible values are: debug, info, warning, "
7272
"error, critical.")
73+
("logtarget", po::value<std::string>(),
74+
"This is the path to the folder where the logging output files will be written. "
75+
"If omitted, the output will be on the console only.")
7376
("jobs,j", po::value<int>()->default_value(4),
7477
"Number of threads the parsers can use.")
7578
("skip,s", po::value<std::vector<std::string>>(),
@@ -219,7 +222,7 @@ int main(int argc, char* argv[])
219222

220223
cc::parser::PluginHandler pHandler(PARSER_PLUGIN_DIR);
221224

222-
cc::util::initLogger();
225+
cc::util::initConsoleLogger();
223226

224227
//--- Process command line arguments ---//
225228

@@ -229,6 +232,17 @@ int main(int argc, char* argv[])
229232
po::store(po::command_line_parser(argc, argv)
230233
.options(desc).allow_unregistered().run(), vm);
231234

235+
if (vm.count("logtarget"))
236+
{
237+
vm.at("logtarget").value() = cc::util::getLoggingBase( vm["logtarget"].as<std::string>()
238+
, vm["name"].as<std::string>()
239+
);
240+
if (!cc::util::initFileLogger(vm["logtarget"].as<std::string>() + "parser.log"))
241+
{
242+
vm.at("logtarget").value() = std::string();
243+
}
244+
}
245+
232246
//--- Skip parser list ---//
233247

234248
std::vector<std::string> skipParserList;

plugins/search/common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_jar(searchcommonjava
99
${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/analysis/Location.java
1010
${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/analysis/SourceTextAnalyzer.java
1111
${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/analysis/SourceTextTokenizer.java
12+
${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/common/FileLoggerInitializer.java
1213
${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/common/IndexFields.java
1314
${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/common/SuggestionDatabase.java
1415
${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/common/NFSFriendlyLockFactory.java
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cc.search.common;
2+
3+
import cc.search.common.config.CommonOptions;
4+
5+
import java.util.logging.Level;
6+
import java.util.logging.Logger;
7+
import java.util.logging.FileHandler;
8+
import java.util.logging.SimpleFormatter;
9+
10+
/**
11+
* Adds file to logging when needed
12+
*/
13+
public class FileLoggerInitializer {
14+
public static void addFileOutput(CommonOptions options_, Logger log_, String pluginName_) {
15+
String filePathField = options_.logFilePath + pluginName_ + ".log";
16+
if (!options_.logFilePath.isEmpty()) {
17+
try {
18+
FileHandler fileHandler = new FileHandler(filePathField, false);
19+
SimpleFormatter formatter = new SimpleFormatter();
20+
fileHandler.setFormatter(formatter);
21+
log_.addHandler(fileHandler);
22+
log_.info("Logging started to file: " + filePathField);
23+
} catch (Exception ex) {
24+
log_.log(Level.WARNING, "Could not add logs to file: " + filePathField, ex);
25+
}
26+
}
27+
}
28+
}

plugins/search/common/src/cc/search/common/config/CommonOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public abstract class CommonOptions {
1616
* Index database path
1717
*/
1818
public String indexDirPath;
19+
/**
20+
* Logging file path
21+
* (if there is no file logging, empty string)
22+
*/
23+
public String logFilePath = "";
1924
/**
2025
* Input file descriptor for thrift IPC.
2126
*/
@@ -77,6 +82,15 @@ protected void setFromCommandLineArguments(List<String> args_)
7782
argIter.remove();
7883
}
7984
break;
85+
case "-logTarget":
86+
if (!argIter.hasNext()) {
87+
throw new InvalidValueException("No path for -logTarget");
88+
} else {
89+
argIter.remove();
90+
logFilePath = argIter.next();
91+
argIter.remove();
92+
}
93+
break;
8094
case "-ipcInFd":
8195
if (!argIter.hasNext()) {
8296
throw new InvalidValueException("-ipcInFd is empty");
@@ -136,6 +150,7 @@ public static String getUsage() {
136150
+ "\t-ipcInFd fd\n\t\tFile descriptor for IPC IN.\n"
137151
+ "\t-ipcOutFd id\n\t\tFile descriptor for IPC OUT.\n"
138152
+ "\t-useSimpleFileLock\n\t\tUse NFS friendly file locks.\n"
153+
+ "\t-logTarget\n\t\tPath to logging file.\n"
139154
+ "\t-cleanupLocks\n\t\tCleanup locks before first lock..\n";
140155
}
141156
}

plugins/search/common/src/cc/search/common/config/LogConfigurator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public LogConfigurator() {
2525
logManager.readConfiguration(cfgStream);
2626

2727
} catch (IOException | SecurityException ex) {
28-
Logger.getLogger(LogConfigurator.class.getName()).log(Level.SEVERE,
28+
Logger.getGlobal().log(Level.SEVERE,
2929
"Failed to configure custom log propeties!", ex);
3030
}
3131
}

plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public class IPCProcessor implements AutoCloseable {
2222
/**
2323
* Logger.
2424
*/
25-
private static final Logger _log = Logger.getLogger(
26-
IPCProcessor.class.getName());
25+
private static final Logger _log = Logger.getGlobal();
2726
/**
2827
* Processor for thrift messages.
2928
*/

plugins/search/indexer/include/indexer/indexerprocess.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class IndexerProcess :
6464
const std::string& indexDatabase_,
6565
const std::string& compassRoot_,
6666
OpenMode openMode_,
67-
LockMode lockMode_ = LockMode::Simple);
67+
LockMode lockMode_ = LockMode::Simple,
68+
const std::string& logTarget_ = "");
6869

6970
/**
7071
* Closes the I/O pipe so the child process will exit if it finished. Also

plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public final class SourceAnalyzer extends AnalyzerWrapper {
2020
/**
2121
* Logger.
2222
*/
23-
private static final Logger _log = Logger.getLogger(SourceAnalyzer.class
24-
.getName());
23+
private static final Logger _log = Logger.getGlobal();
2524
/**
2625
* Analyzer for a full path.
2726
*/

plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class CTags implements TagGenerator {
1919
/**
2020
* Logger.
2121
*/
22-
private final static Logger _log = Logger.getLogger(CTags.class
23-
.getName());
22+
private final static Logger _log = Logger.getGlobal();
2423
/**
2524
* Filter terminator string for ctags.
2625
*/

0 commit comments

Comments
 (0)