From 568b604721aba02c64f57db5c9f31201e5826523 Mon Sep 17 00:00:00 2001 From: Tamas Dunai Date: Tue, 11 Oct 2022 08:37:48 +0200 Subject: [PATCH 1/9] Add file logging option --- parser/src/parser.cpp | 15 ++++++- plugins/search/common/CMakeLists.txt | 1 + .../search/common/FileLoggerInitializer.java | 41 +++++++++++++++++++ .../search/common/config/CommonOptions.java | 15 +++++++ .../cc/search/common/ipc/IPCProcessor.java | 6 +++ .../indexer/include/indexer/indexerprocess.h | 3 +- .../src/cc/search/indexer/app/Indexer.java | 4 ++ plugins/search/indexer/src/indexerprocess.cpp | 6 ++- plugins/search/parser/src/searchparser.cpp | 6 ++- .../service/include/service/serviceprocess.h | 4 +- .../search/service/app/SearchAppCommon.java | 4 ++ .../cc/search/service/app/query/QueryApp.java | 3 ++ .../service/app/service/SearchHandler.java | 4 ++ .../service/app/service/ServiceApp.java | 19 +++++++-- .../search/suggestion/SuggestionHandler.java | 4 ++ plugins/search/service/src/searchservice.cpp | 5 ++- util/include/util/logutil.h | 4 +- util/src/logutil.cpp | 38 +++++++++++++++-- webserver/src/webserver.cpp | 15 ++++++- 19 files changed, 179 insertions(+), 18 deletions(-) create mode 100644 plugins/search/common/src/cc/search/common/FileLoggerInitializer.java diff --git a/parser/src/parser.cpp b/parser/src/parser.cpp index 4c263d15b..0b492dc39 100644 --- a/parser/src/parser.cpp +++ b/parser/src/parser.cpp @@ -82,7 +82,10 @@ po::options_description commandLineArguments() "further actions modifying the state of the database.") ("incremental-threshold", po::value()->default_value(10), "This is a threshold percentage. If the total ratio of changed files " - "is greater than this value, full parse is forced instead of incremental parsing."); + "is greater than this value, full parse is forced instead of incremental parsing.") + ("log-target", po::value(), + "This is the path to the file where the logging output will be written. " + "If omitted, the output will be on the console only."); return desc; } @@ -219,7 +222,7 @@ int main(int argc, char* argv[]) cc::parser::PluginHandler pHandler(PARSER_PLUGIN_DIR); - cc::util::initLogger(); + cc::util::initConsoleLogger(); //--- Process command line arguments ---// @@ -229,6 +232,14 @@ int main(int argc, char* argv[]) po::store(po::command_line_parser(argc, argv) .options(desc).allow_unregistered().run(), vm); + if (vm.count("log-target")) + { + if(!cc::util::initFileLogger(vm["log-target"].as())) + { + vm.at("log-target").value() = std::string(""); + } + } + //--- Skip parser list ---// std::vector skipParserList; diff --git a/plugins/search/common/CMakeLists.txt b/plugins/search/common/CMakeLists.txt index 5c1b900c1..50bb6a261 100644 --- a/plugins/search/common/CMakeLists.txt +++ b/plugins/search/common/CMakeLists.txt @@ -9,6 +9,7 @@ add_jar(searchcommonjava ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/analysis/Location.java ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/analysis/SourceTextAnalyzer.java ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/analysis/SourceTextTokenizer.java + ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/common/FileLoggerInitializer.java ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/common/IndexFields.java ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/common/SuggestionDatabase.java ${CMAKE_CURRENT_SOURCE_DIR}/src/cc/search/common/NFSFriendlyLockFactory.java diff --git a/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java b/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java new file mode 100644 index 000000000..fd000fc78 --- /dev/null +++ b/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java @@ -0,0 +1,41 @@ +package cc.search.common; + +import cc.search.common.config.CommonOptions; + +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.FileHandler; +import java.util.logging.SimpleFormatter; + +/** + * Adds file to logging when needed + */ +public class FileLoggerInitializer implements Runnable { + /** + * The name of the "path" field. Path contains the full path of the file. + */ + private static String _filePathField = ""; + /** + * The logger that needs the file handler + */ + private static Logger _log; + + public FileLoggerInitializer(CommonOptions options_, Logger log_) { + _filePathField = options_.logFilePath; + _log = log_; + } + + public void run() { + if (!_filePathField.isEmpty()) { + try { + FileHandler fileHandler = new FileHandler(_filePathField, true); // append mode + SimpleFormatter formatter = new SimpleFormatter(); + fileHandler.setFormatter(formatter); + _log.addHandler(fileHandler); + _log.info("Logging started to file: " + _filePathField); + } catch (Exception ex) { + _log.log(Level.WARNING, "Could not add logs to file: " + _filePathField, ex); + } + } + } +} diff --git a/plugins/search/common/src/cc/search/common/config/CommonOptions.java b/plugins/search/common/src/cc/search/common/config/CommonOptions.java index f94d63dfa..908ea06d4 100644 --- a/plugins/search/common/src/cc/search/common/config/CommonOptions.java +++ b/plugins/search/common/src/cc/search/common/config/CommonOptions.java @@ -16,6 +16,11 @@ public abstract class CommonOptions { * Index database path */ public String indexDirPath; + /** + * Logging file path + * (if there is no file logging, empty string) + */ + public String logFilePath = ""; /** * Input file descriptor for thrift IPC. */ @@ -77,6 +82,15 @@ protected void setFromCommandLineArguments(List args_) argIter.remove(); } break; + case "-logTarget": + if (!argIter.hasNext()) { + throw new InvalidValueException("No path for -logTarget"); + } else { + argIter.remove(); + logFilePath = argIter.next(); + argIter.remove(); + } + break; case "-ipcInFd": if (!argIter.hasNext()) { throw new InvalidValueException("-ipcInFd is empty"); @@ -136,6 +150,7 @@ public static String getUsage() { + "\t-ipcInFd fd\n\t\tFile descriptor for IPC IN.\n" + "\t-ipcOutFd id\n\t\tFile descriptor for IPC OUT.\n" + "\t-useSimpleFileLock\n\t\tUse NFS friendly file locks.\n" + + "\t-logTarget\n\t\tPath to logging file.\n" + "\t-cleanupLocks\n\t\tCleanup locks before first lock..\n"; } } \ No newline at end of file diff --git a/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java b/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java index 553eb6cd6..d566d96e4 100644 --- a/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java +++ b/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java @@ -1,11 +1,14 @@ package cc.search.common.ipc; +import cc.search.common.FileLoggerInitializer; import cc.search.common.config.CommonOptions; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.logging.FileHandler; +import java.util.logging.SimpleFormatter; import org.apache.thrift.TException; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; @@ -59,6 +62,9 @@ public class IPCProcessor implements AutoCloseable { public IPCProcessor(CommonOptions options_, TProcessor processor_) throws FileNotFoundException { _processor = processor_; + + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(options_, _log); + addFileLogger.run(); TProtocolFactory factory = new TBinaryProtocol.Factory(); _inTransport = new TIOStreamTransport( diff --git a/plugins/search/indexer/include/indexer/indexerprocess.h b/plugins/search/indexer/include/indexer/indexerprocess.h index f33fca1cf..7de66a18a 100644 --- a/plugins/search/indexer/include/indexer/indexerprocess.h +++ b/plugins/search/indexer/include/indexer/indexerprocess.h @@ -64,7 +64,8 @@ class IndexerProcess : const std::string& indexDatabase_, const std::string& compassRoot_, OpenMode openMode_, - LockMode lockMode_ = LockMode::Simple); + LockMode lockMode_ = LockMode::Simple, + const std::string& logTarget_ = ""); /** * Closes the I/O pipe so the child process will exit if it finished. Also diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java index 0f4d234ff..1b2b993bf 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java @@ -4,6 +4,7 @@ import cc.parser.search.IndexerService; import cc.search.analysis.SourceAnalyzer; import cc.search.analysis.tags.TagGeneratorManager; +import cc.search.common.FileLoggerInitializer; import cc.search.common.ipc.IPCProcessor; import cc.search.common.config.InvalidValueException; import cc.search.common.config.UnknownArgumentException; @@ -82,6 +83,9 @@ public class Indexer implements AutoCloseable, IndexerService.Iface { private Indexer(Options options_) throws IOException { _options = options_; + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(_options, _log); + addFileLogger.run(); + try { _indexDir = FSDirectory.open(new File(_options.indexDirPath), _options.createLockFactory()); diff --git a/plugins/search/indexer/src/indexerprocess.cpp b/plugins/search/indexer/src/indexerprocess.cpp index 1534eeac4..b1f92caef 100644 --- a/plugins/search/indexer/src/indexerprocess.cpp +++ b/plugins/search/indexer/src/indexerprocess.cpp @@ -30,7 +30,8 @@ IndexerProcess::IndexerProcess( const std::string& indexDatabase_, const std::string& compassRoot_, IndexerProcess::OpenMode openMode_, - IndexerProcess::LockMode lockMode_) + IndexerProcess::LockMode lockMode_, + const std::string& logTarget_) { openPipe(_pipeFd2[0], _pipeFd2[1]); @@ -65,7 +66,8 @@ IndexerProcess::IndexerProcess( "cc.search.indexer.app.Indexer", "-indexDB", indexDatabase_.c_str(), "-ipcInFd", inFd.c_str(), - "-ipcOutFd", outFd.c_str() + "-ipcOutFd", outFd.c_str(), + "-logTarget", logTarget_.c_str() }; switch (openMode_) diff --git a/plugins/search/parser/src/searchparser.cpp b/plugins/search/parser/src/searchparser.cpp index a0a79de4b..62a025eb0 100644 --- a/plugins/search/parser/src/searchparser.cpp +++ b/plugins/search/parser/src/searchparser.cpp @@ -70,7 +70,11 @@ SearchParser::SearchParser(ParserContext& ctx_) : AbstractParser(ctx_), _indexProcess.reset(new IndexerProcess( _searchDatabase, ctx_.compassRoot, - IndexerProcess::OpenMode::Create)); + IndexerProcess::OpenMode::Create, + IndexerProcess::LockMode::Simple, + ctx_.options.count("log-target") + ? ctx_.options["log-target"].as() + : "")); } catch (const IndexerProcess::Failure& ex_) { diff --git a/plugins/search/service/include/service/serviceprocess.h b/plugins/search/service/include/service/serviceprocess.h index c8e85c2c6..3a61d3233 100644 --- a/plugins/search/service/include/service/serviceprocess.h +++ b/plugins/search/service/include/service/serviceprocess.h @@ -33,7 +33,8 @@ class ServiceProcess : public SearchServiceIf, public util::PipedProcess * @param indexDatabase_ path to a index database */ ServiceProcess(const std::string& indexDatabase_, - const std::string& compassRoot_) : + const std::string& compassRoot_, + const std::string& logTarget_ = "") : _indexDatabase(indexDatabase_) { openPipe(_pipeFd2[0], _pipeFd2[1]); @@ -71,6 +72,7 @@ class ServiceProcess : public SearchServiceIf, public util::PipedProcess "-ipcOutFd", outFd.c_str(), "-useSimpleFileLock", "-cleanupLocks", + "-logTarget", logTarget_.c_str(), nullptr); LOG(error) << "execlp failed!"; diff --git a/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java b/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java index 696b1e2f2..a406d074b 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java @@ -3,6 +3,7 @@ import cc.search.analysis.QueryAnalyzer; import cc.search.common.config.CommonOptions; import cc.search.common.IndexFields; +import cc.search.common.FileLoggerInitializer; import cc.search.match.Context; import cc.search.match.QueryContext; import cc.search.match.matcher.MasterMatcherFactory; @@ -227,6 +228,9 @@ public int compare(LineMatch line1_, LineMatch line2_) { protected SearchAppCommon(CommonOptions options_) throws IOException { _options = options_; + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(_options, _log); + addFileLogger.run(); + try { _indexReader = DirectoryReader.open(FSDirectory.open( new File(_options.indexDirPath), _options.createLockFactory())); diff --git a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java index b80a90aff..ccead49e3 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java @@ -1,6 +1,7 @@ package cc.search.service.app.query; import cc.search.service.app.SearchAppCommon; +import cc.search.common.FileLoggerInitializer; import cc.search.common.config.InvalidValueException; import cc.search.common.config.UnknownArgumentException; import cc.search.match.QueryContext; @@ -35,6 +36,8 @@ public class QueryApp extends SearchAppCommon { private QueryApp(QueryAppOptions options_) throws IOException { super(options_); _appOptions = options_; + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(_appOptions, _log); + addFileLogger.run(); } /** diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java b/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java index 04ac6b763..ed228aa11 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java @@ -1,5 +1,6 @@ package cc.search.service.app.service; +import cc.search.common.FileLoggerInitializer; import cc.search.analysis.AdvancedTagQueryParser; import cc.search.analysis.QueryAnalyzer; import cc.search.analysis.log.LogQueryBuilder; @@ -146,6 +147,9 @@ private SearchResult runSearch(QueryContext context_, SearchParams params_) */ public SearchHandler(CommonOptions options_) throws IOException { super(options_); + + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(options_, _log); + addFileLogger.run(); Analyzer analyzer = new QueryAnalyzer(); diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java index 99ede1dca..8d5e85c5a 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java @@ -1,5 +1,6 @@ package cc.search.service.app.service; +import cc.search.common.FileLoggerInitializer; import cc.search.common.ipc.IPCProcessor; import cc.search.common.config.InvalidValueException; import cc.search.common.config.UnknownArgumentException; @@ -7,6 +8,9 @@ import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.logging.FileHandler; +import java.util.logging.SimpleFormatter; +import java.util.Arrays; /** * The search service application. @@ -45,20 +49,27 @@ public void close() { } public static void main(String[] args_) { - _log.info("Search server started!"); - try { + // Initialize file logger + ServiceAppOptions options = new ServiceAppOptions(args_); + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(options, _log); + addFileLogger.run(); + + _log.info("Search server started!"); try (ServiceApp app = - new ServiceApp(new ServiceAppOptions(args_))) { + new ServiceApp(options)) { _log.log(Level.INFO, "Start serving search for index {0}", app._options.indexDirPath); app._processor.serve(); - } catch (UnknownArgumentException | InvalidValueException | IOException e) { + } catch (IOException e) { _log.log(Level.SEVERE, "Fatal error!", e); System.exit(-1); } + } catch (UnknownArgumentException | InvalidValueException e) { + _log.log(Level.SEVERE, "Fatal error!", e); + System.exit(-1); } catch (Exception ex) { _log.log(Level.SEVERE, "Something bad happened :-(", ex); } finally { diff --git a/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java b/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java index a0f128e7c..dfb4633ed 100644 --- a/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java +++ b/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java @@ -1,5 +1,6 @@ package cc.search.suggestion; +import cc.search.common.FileLoggerInitializer; import cc.search.common.SuggestionDatabase; import cc.search.common.config.CommonOptions; import java.io.IOException; @@ -43,6 +44,9 @@ public final class SuggestionHandler implements AutoCloseable { * @param opts_ the program options. */ public void loadDatabases(CommonOptions opts_) { + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(opts_, _log); + addFileLogger.run(); + try { loadFileNameSuggester(opts_); } catch (IOException ex) { diff --git a/plugins/search/service/src/searchservice.cpp b/plugins/search/service/src/searchservice.cpp index b83b6415d..e2dd9d1ae 100644 --- a/plugins/search/service/src/searchservice.cpp +++ b/plugins/search/service/src/searchservice.cpp @@ -106,7 +106,10 @@ SearchServiceHandler::SearchServiceHandler( _db(db_) { _javaProcess.reset(new ServiceProcess(*datadir_ + "/search", - context_.compassRoot)); + context_.compassRoot, + context_.options.count("log-target") + ? context_.options["log-target"].as() + : "")); } void SearchServiceHandler::search( diff --git a/util/include/util/logutil.h b/util/include/util/logutil.h index 2127e7566..aa76d1d51 100644 --- a/util/include/util/logutil.h +++ b/util/include/util/logutil.h @@ -10,7 +10,9 @@ namespace cc namespace util { -void initLogger(); +void initConsoleLogger(); + +bool initFileLogger(const std::string& path_); boost::log::trivial::severity_level getSeverityLevel(); diff --git a/util/src/logutil.cpp b/util/src/logutil.cpp index 445558856..bf74c873b 100644 --- a/util/src/logutil.cpp +++ b/util/src/logutil.cpp @@ -12,7 +12,7 @@ namespace util namespace { -void logFormatter( +void consoleLogFormatter( const boost::log::record_view& rec, boost::log::formatting_ostream& strm) { auto severity = rec[boost::log::trivial::severity]; @@ -49,8 +49,19 @@ void logFormatter( } } +void fileLogFormatter( + const boost::log::record_view& rec, boost::log::formatting_ostream& strm) +{ + auto severity = rec[boost::log::trivial::severity]; + + std::string sLevel = boost::log::trivial::to_string(severity.get()); + std::transform(sLevel.begin(), sLevel.end(), sLevel.begin(), ::toupper); + + strm << "[" << sLevel << "] " << rec[boost::log::expressions::smessage]; } +} // namespace + boost::log::trivial::severity_level getSeverityLevel() { return boost::log::attribute_cast< @@ -58,13 +69,34 @@ boost::log::trivial::severity_level getSeverityLevel() boost::log::core::get()->get_global_attributes()["Severity"]).get(); } -void initLogger() +void initConsoleLogger() { auto fsSink = boost::log::add_console_log( std::cout, boost::log::keywords::auto_flush = true); - fsSink->set_formatter(&logFormatter); + fsSink->set_formatter(&consoleLogFormatter); +} + +bool initFileLogger(const std::string& path_) +{ + auto fsSink = boost::log::add_file_log( + boost::log::keywords::file_name = path_, + boost::log::keywords::auto_flush = true + ); + fsSink->set_formatter(&fileLogFormatter); + try + { + // check if logging to file is possible + LOG(info) << "Start logging in file: " << path_; + } + catch(...) + { + boost::log::core::get()->remove_sink(fsSink); + LOG(warning) << "Could not open file for logging: " << path_; + return false; + } + return true; } } // util diff --git a/webserver/src/webserver.cpp b/webserver/src/webserver.cpp index c0df38c42..96d8c9d24 100644 --- a/webserver/src/webserver.cpp +++ b/webserver/src/webserver.cpp @@ -38,7 +38,10 @@ po::options_description commandLineArguments() "Logging level of the parser. Possible values are: debug, info, warning, " "error, critical") ("jobs,j", po::value()->default_value(4), - "Number of worker threads."); + "Number of worker threads.") + ("log-target", po::value(), + "This is the path to the file where the logging output will be written. " + "If omitted, the output will be on the console only."); return desc; } @@ -51,7 +54,7 @@ int main(int argc, char* argv[]) const std::string SERVICE_PLUGIN_DIR = compassRoot + "/lib/serviceplugin"; const std::string WEBGUI_DIR = compassRoot + "/share/codecompass/webgui/"; - cc::util::initLogger(); + cc::util::initConsoleLogger(); MainRequestHandler requestHandler; requestHandler.pluginHandler.addDirectory(SERVICE_PLUGIN_DIR); @@ -67,6 +70,14 @@ int main(int argc, char* argv[]) po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); + if (vm.count("log-target")) + { + if(!cc::util::initFileLogger(vm["log-target"].as())) + { + vm.at("log-target").value() = std::string(""); + } + } + if (argc < 2 || vm.count("help")) { std::cout << desc << std::endl; From a62790a2e5fbadd60e43edf4409a128d4d3cca4f Mon Sep 17 00:00:00 2001 From: Tamas Dunai Date: Sun, 16 Oct 2022 21:17:01 +0200 Subject: [PATCH 2/9] Logs of different plugins go to different files. --- .../src/cc/search/common/FileLoggerInitializer.java | 6 +++--- .../src/cc/search/common/config/LogConfigurator.java | 2 +- .../common/src/cc/search/common/ipc/IPCProcessor.java | 9 +-------- .../src/cc/search/analysis/SourceAnalyzer.java | 3 +-- .../indexer-java/src/cc/search/analysis/tags/CTags.java | 3 +-- .../src/cc/search/analysis/tags/SourceTagGenerator.java | 3 +-- .../src/cc/search/analysis/tags/TagGeneratorManager.java | 3 +-- .../src/cc/search/indexer/AbstractIndexer.java | 3 +-- .../indexer-java/src/cc/search/indexer/FileIndexer.java | 3 +-- .../indexer-java/src/cc/search/indexer/app/Indexer.java | 4 ++-- .../src/cc/search/suggestion/DatabaseBuilder.java | 3 +-- .../src/cc/search/analysis/QueryAnalyzer.java | 3 +-- .../src/cc/search/analysis/query/HIPDQuery.java | 3 +-- .../cc/search/match/matcher/MasterMatcherFactory.java | 3 +-- .../cc/search/match/matcher/TagKindMatcherFactory.java | 3 +-- .../src/cc/search/service/app/SearchAppCommon.java | 7 +------ .../src/cc/search/service/app/query/QueryApp.java | 5 +---- .../src/cc/search/service/app/service/SearchHandler.java | 7 +------ .../src/cc/search/service/app/service/ServiceApp.java | 8 ++------ .../src/cc/search/suggestion/SuggestionHandler.java | 7 +------ 20 files changed, 24 insertions(+), 64 deletions(-) diff --git a/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java b/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java index fd000fc78..f8efca5f8 100644 --- a/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java +++ b/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java @@ -10,7 +10,7 @@ /** * Adds file to logging when needed */ -public class FileLoggerInitializer implements Runnable { +public class FileLoggerInitializer { /** * The name of the "path" field. Path contains the full path of the file. */ @@ -20,8 +20,8 @@ public class FileLoggerInitializer implements Runnable { */ private static Logger _log; - public FileLoggerInitializer(CommonOptions options_, Logger log_) { - _filePathField = options_.logFilePath; + public FileLoggerInitializer(CommonOptions options_, Logger log_, String pluginName_) { + _filePathField = options_.logFilePath + "." + pluginName_; _log = log_; } diff --git a/plugins/search/common/src/cc/search/common/config/LogConfigurator.java b/plugins/search/common/src/cc/search/common/config/LogConfigurator.java index 11ff27694..f56e7e600 100644 --- a/plugins/search/common/src/cc/search/common/config/LogConfigurator.java +++ b/plugins/search/common/src/cc/search/common/config/LogConfigurator.java @@ -25,7 +25,7 @@ public LogConfigurator() { logManager.readConfiguration(cfgStream); } catch (IOException | SecurityException ex) { - Logger.getLogger(LogConfigurator.class.getName()).log(Level.SEVERE, + Logger.getLogger("GLOBAL_LOGGER").log(Level.SEVERE, "Failed to configure custom log propeties!", ex); } } diff --git a/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java b/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java index d566d96e4..782d6aebd 100644 --- a/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java +++ b/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java @@ -1,14 +1,11 @@ package cc.search.common.ipc; -import cc.search.common.FileLoggerInitializer; import cc.search.common.config.CommonOptions; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.logging.FileHandler; -import java.util.logging.SimpleFormatter; import org.apache.thrift.TException; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; @@ -25,8 +22,7 @@ public class IPCProcessor implements AutoCloseable { /** * Logger. */ - private static final Logger _log = Logger.getLogger( - IPCProcessor.class.getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Processor for thrift messages. */ @@ -62,9 +58,6 @@ public class IPCProcessor implements AutoCloseable { public IPCProcessor(CommonOptions options_, TProcessor processor_) throws FileNotFoundException { _processor = processor_; - - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(options_, _log); - addFileLogger.run(); TProtocolFactory factory = new TBinaryProtocol.Factory(); _inTransport = new TIOStreamTransport( diff --git a/plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java b/plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java index c67edbffc..573b53d18 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java @@ -20,8 +20,7 @@ public final class SourceAnalyzer extends AnalyzerWrapper { /** * Logger. */ - private static final Logger _log = Logger.getLogger(SourceAnalyzer.class - .getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Analyzer for a full path. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java index 3a603000b..f0b0dc2d7 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java @@ -19,8 +19,7 @@ class CTags implements TagGenerator { /** * Logger. */ - private final static Logger _log = Logger.getLogger(CTags.class - .getName()); + private final static Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Filter terminator string for ctags. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/SourceTagGenerator.java b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/SourceTagGenerator.java index 044148da7..b8ff6aedc 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/SourceTagGenerator.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/SourceTagGenerator.java @@ -21,8 +21,7 @@ public class SourceTagGenerator implements TagGenerator { /** * Logger. */ - private static final Logger _log = Logger.getLogger(SourceTagGenerator.class - .getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Options array for initializing _artf460600CTags. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/TagGeneratorManager.java b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/TagGeneratorManager.java index 4df24fa49..c5452298f 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/TagGeneratorManager.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/TagGeneratorManager.java @@ -14,8 +14,7 @@ public final class TagGeneratorManager implements AutoCloseable { /** * Logger. */ - private final static Logger _log = Logger.getLogger( - TagGeneratorManager.class.getName()); + private final static Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Singleton instance. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/AbstractIndexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/AbstractIndexer.java index 8501b7ed4..05eb0b81e 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/AbstractIndexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/AbstractIndexer.java @@ -42,8 +42,7 @@ abstract public class AbstractIndexer { /** * Logger. */ - private static final Logger _log = Logger.getLogger(AbstractIndexer - .class.getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Field type for storing tags. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/FileIndexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/FileIndexer.java index 0c0d36e89..e1b56fcc2 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/FileIndexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/FileIndexer.java @@ -16,8 +16,7 @@ public class FileIndexer extends AbstractIndexer { /** * Logger. */ - private static final Logger _log = Logger.getLogger(FileIndexer.class - .getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * File path to index. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java index 1b2b993bf..4e023251a 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java @@ -41,7 +41,7 @@ public class Indexer implements AutoCloseable, IndexerService.Iface { /** * Logger. */ - private static final Logger _log = Logger.getLogger(Indexer.class.getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Command line options. */ @@ -83,7 +83,7 @@ public class Indexer implements AutoCloseable, IndexerService.Iface { private Indexer(Options options_) throws IOException { _options = options_; - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(_options, _log); + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(_options, _log, "indexPlugin"); addFileLogger.run(); try { diff --git a/plugins/search/indexer/indexer-java/src/cc/search/suggestion/DatabaseBuilder.java b/plugins/search/indexer/indexer-java/src/cc/search/suggestion/DatabaseBuilder.java index d1a44683b..cb3d7d2c9 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/suggestion/DatabaseBuilder.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/suggestion/DatabaseBuilder.java @@ -27,8 +27,7 @@ public final class DatabaseBuilder { /** * Logger. */ - private static final Logger _log - = Logger.getLogger(DatabaseBuilder.class.getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * An index reader for the main index database. */ diff --git a/plugins/search/service/search-java/src/cc/search/analysis/QueryAnalyzer.java b/plugins/search/service/search-java/src/cc/search/analysis/QueryAnalyzer.java index 400f1af2c..6ea24bd6e 100644 --- a/plugins/search/service/search-java/src/cc/search/analysis/QueryAnalyzer.java +++ b/plugins/search/service/search-java/src/cc/search/analysis/QueryAnalyzer.java @@ -17,8 +17,7 @@ public class QueryAnalyzer extends AnalyzerWrapper { /** * Logger. */ - private static final Logger _log = Logger.getLogger(QueryAnalyzer.class. - getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * A simple analyzer for queries. */ diff --git a/plugins/search/service/search-java/src/cc/search/analysis/query/HIPDQuery.java b/plugins/search/service/search-java/src/cc/search/analysis/query/HIPDQuery.java index b4d8c2f58..074381f13 100644 --- a/plugins/search/service/search-java/src/cc/search/analysis/query/HIPDQuery.java +++ b/plugins/search/service/search-java/src/cc/search/analysis/query/HIPDQuery.java @@ -50,8 +50,7 @@ public class HIPDQuery extends Query { /** * Logger. */ - private static final Logger _log = Logger.getLogger(HIPDQuery.class. - getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * The default value of the maximal allowed difference. */ diff --git a/plugins/search/service/search-java/src/cc/search/match/matcher/MasterMatcherFactory.java b/plugins/search/service/search-java/src/cc/search/match/matcher/MasterMatcherFactory.java index 951398d85..b48f0f601 100644 --- a/plugins/search/service/search-java/src/cc/search/match/matcher/MasterMatcherFactory.java +++ b/plugins/search/service/search-java/src/cc/search/match/matcher/MasterMatcherFactory.java @@ -15,8 +15,7 @@ public class MasterMatcherFactory implements ResultMatcherFactory { /** * Logger. */ - private static final Logger _log = Logger.getLogger(MasterMatcherFactory. - class.getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Matcher wrapper for matching with multiple matchers. */ diff --git a/plugins/search/service/search-java/src/cc/search/match/matcher/TagKindMatcherFactory.java b/plugins/search/service/search-java/src/cc/search/match/matcher/TagKindMatcherFactory.java index 9356e845b..924ab5022 100644 --- a/plugins/search/service/search-java/src/cc/search/match/matcher/TagKindMatcherFactory.java +++ b/plugins/search/service/search-java/src/cc/search/match/matcher/TagKindMatcherFactory.java @@ -22,8 +22,7 @@ class TagKindMatcherFactory implements ResultMatcherFactory { /** * Logger. */ - private static final Logger _log = Logger.getLogger(TagKindMatcherFactory. - class.getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Token filter for filtering relevant tokens by its kind. */ diff --git a/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java b/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java index a406d074b..cef069905 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java @@ -3,7 +3,6 @@ import cc.search.analysis.QueryAnalyzer; import cc.search.common.config.CommonOptions; import cc.search.common.IndexFields; -import cc.search.common.FileLoggerInitializer; import cc.search.match.Context; import cc.search.match.QueryContext; import cc.search.match.matcher.MasterMatcherFactory; @@ -76,8 +75,7 @@ public abstract class SearchAppCommon implements AutoCloseable { /** * Logger. */ - private static final Logger _log = Logger.getLogger(SearchAppCommon.class - .getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Async task class for matching lines in a document. @@ -228,9 +226,6 @@ public int compare(LineMatch line1_, LineMatch line2_) { protected SearchAppCommon(CommonOptions options_) throws IOException { _options = options_; - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(_options, _log); - addFileLogger.run(); - try { _indexReader = DirectoryReader.open(FSDirectory.open( new File(_options.indexDirPath), _options.createLockFactory())); diff --git a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java index ccead49e3..f223dd07b 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java @@ -1,7 +1,6 @@ package cc.search.service.app.query; import cc.search.service.app.SearchAppCommon; -import cc.search.common.FileLoggerInitializer; import cc.search.common.config.InvalidValueException; import cc.search.common.config.UnknownArgumentException; import cc.search.match.QueryContext; @@ -23,7 +22,7 @@ public class QueryApp extends SearchAppCommon { /** * Logger. */ - private static final Logger _log = Logger.getLogger(QueryApp.class.getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Application options. */ @@ -36,8 +35,6 @@ public class QueryApp extends SearchAppCommon { private QueryApp(QueryAppOptions options_) throws IOException { super(options_); _appOptions = options_; - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(_appOptions, _log); - addFileLogger.run(); } /** diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java b/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java index ed228aa11..3e929de87 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java @@ -1,6 +1,5 @@ package cc.search.service.app.service; -import cc.search.common.FileLoggerInitializer; import cc.search.analysis.AdvancedTagQueryParser; import cc.search.analysis.QueryAnalyzer; import cc.search.analysis.log.LogQueryBuilder; @@ -50,8 +49,7 @@ abstract class SearchHandler extends SearchAppCommon /** * Logger. */ - private static final Logger _log = Logger.getLogger(SearchHandler.class - .getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Handler for suggestion requests. */ @@ -148,9 +146,6 @@ private SearchResult runSearch(QueryContext context_, SearchParams params_) public SearchHandler(CommonOptions options_) throws IOException { super(options_); - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(options_, _log); - addFileLogger.run(); - Analyzer analyzer = new QueryAnalyzer(); _advDefQueryParser = new AdvancedTagQueryParser(analyzer); diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java index 8d5e85c5a..845b4ff72 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java @@ -8,9 +8,6 @@ import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.logging.FileHandler; -import java.util.logging.SimpleFormatter; -import java.util.Arrays; /** * The search service application. @@ -19,8 +16,7 @@ public class ServiceApp extends SearchHandler { /** * Logger. */ - private static final Logger _log = Logger.getLogger(ServiceApp.class - .getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * IPC message processor. */ @@ -52,7 +48,7 @@ public static void main(String[] args_) { try { // Initialize file logger ServiceAppOptions options = new ServiceAppOptions(args_); - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(options, _log); + FileLoggerInitializer addFileLogger = new FileLoggerInitializer(options, _log, "searchPlugin"); addFileLogger.run(); _log.info("Search server started!"); diff --git a/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java b/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java index dfb4633ed..8a1b7aaf6 100644 --- a/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java +++ b/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java @@ -1,6 +1,5 @@ package cc.search.suggestion; -import cc.search.common.FileLoggerInitializer; import cc.search.common.SuggestionDatabase; import cc.search.common.config.CommonOptions; import java.io.IOException; @@ -26,8 +25,7 @@ public final class SuggestionHandler implements AutoCloseable { /** * Logger. */ - private static final Logger _log = Logger.getLogger(SuggestionHandler.class - .getName()); + private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); /** * Filename suggester. */ @@ -44,9 +42,6 @@ public final class SuggestionHandler implements AutoCloseable { * @param opts_ the program options. */ public void loadDatabases(CommonOptions opts_) { - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(opts_, _log); - addFileLogger.run(); - try { loadFileNameSuggester(opts_); } catch (IOException ex) { From dcaf4006fa18be9569f3f308b12f111278742461 Mon Sep 17 00:00:00 2001 From: Tamas Dunai Date: Tue, 1 Nov 2022 16:51:30 +0100 Subject: [PATCH 3/9] Remove append mode from java plugin file logging --- .../search/common/FileLoggerInitializer.java | 27 +++++-------------- .../src/cc/search/indexer/app/Indexer.java | 3 +-- .../service/app/service/ServiceApp.java | 3 +-- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java b/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java index f8efca5f8..40e58e593 100644 --- a/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java +++ b/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java @@ -11,30 +11,17 @@ * Adds file to logging when needed */ public class FileLoggerInitializer { - /** - * The name of the "path" field. Path contains the full path of the file. - */ - private static String _filePathField = ""; - /** - * The logger that needs the file handler - */ - private static Logger _log; - - public FileLoggerInitializer(CommonOptions options_, Logger log_, String pluginName_) { - _filePathField = options_.logFilePath + "." + pluginName_; - _log = log_; - } - - public void run() { - if (!_filePathField.isEmpty()) { + public static void addFileOutput(CommonOptions options_, Logger log_, String pluginName_) { + String filePathField = options_.logFilePath + "." + pluginName_; + if (!pluginName_.isEmpty()) { try { - FileHandler fileHandler = new FileHandler(_filePathField, true); // append mode + FileHandler fileHandler = new FileHandler(filePathField, false); SimpleFormatter formatter = new SimpleFormatter(); fileHandler.setFormatter(formatter); - _log.addHandler(fileHandler); - _log.info("Logging started to file: " + _filePathField); + log_.addHandler(fileHandler); + log_.info("Logging started to file: " + filePathField); } catch (Exception ex) { - _log.log(Level.WARNING, "Could not add logs to file: " + _filePathField, ex); + log_.log(Level.WARNING, "Could not add logs to file: " + filePathField, ex); } } } diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java index 4e023251a..a1618854d 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java @@ -83,8 +83,7 @@ public class Indexer implements AutoCloseable, IndexerService.Iface { private Indexer(Options options_) throws IOException { _options = options_; - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(_options, _log, "indexPlugin"); - addFileLogger.run(); + FileLoggerInitializer.addFileOutput(_options, _log, "indexPlugin"); try { _indexDir = FSDirectory.open(new File(_options.indexDirPath), diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java index 845b4ff72..d73dd3e64 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java @@ -48,8 +48,7 @@ public static void main(String[] args_) { try { // Initialize file logger ServiceAppOptions options = new ServiceAppOptions(args_); - FileLoggerInitializer addFileLogger = new FileLoggerInitializer(options, _log, "searchPlugin"); - addFileLogger.run(); + FileLoggerInitializer.addFileOutput(options, _log, "searchPlugin"); _log.info("Search server started!"); try (ServiceApp app = From a486722def693e294ecb165b9d097b76456c66c0 Mon Sep 17 00:00:00 2001 From: Tamas Dunai Date: Sun, 13 Nov 2022 22:02:35 +0100 Subject: [PATCH 4/9] Use directory instead of file name. Prevent ~ symbol useage --- parser/src/parser.cpp | 7 +++++-- .../src/cc/search/common/FileLoggerInitializer.java | 4 ++-- .../src/cc/search/indexer/app/Indexer.java | 2 +- .../src/cc/search/service/app/query/QueryApp.java | 2 ++ .../cc/search/service/app/service/ServiceApp.java | 2 +- util/include/util/logutil.h | 2 ++ util/src/logutil.cpp | 13 +++++++++++++ webserver/src/webserver.cpp | 8 ++++++-- 8 files changed, 32 insertions(+), 8 deletions(-) diff --git a/parser/src/parser.cpp b/parser/src/parser.cpp index 0b492dc39..700ec73c4 100644 --- a/parser/src/parser.cpp +++ b/parser/src/parser.cpp @@ -84,7 +84,7 @@ po::options_description commandLineArguments() "This is a threshold percentage. If the total ratio of changed files " "is greater than this value, full parse is forced instead of incremental parsing.") ("log-target", po::value(), - "This is the path to the file where the logging output will be written. " + "This is the path to the folder where the logging output files will be written. " "If omitted, the output will be on the console only."); return desc; @@ -234,7 +234,10 @@ int main(int argc, char* argv[]) if (vm.count("log-target")) { - if(!cc::util::initFileLogger(vm["log-target"].as())) + vm.at("log-target").value() = cc::util::getLoggingBase( vm["log-target"].as() + , vm["name"].as() + ); + if (!cc::util::initFileLogger(vm["log-target"].as() + "parser.log")) { vm.at("log-target").value() = std::string(""); } diff --git a/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java b/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java index 40e58e593..606313010 100644 --- a/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java +++ b/plugins/search/common/src/cc/search/common/FileLoggerInitializer.java @@ -12,8 +12,8 @@ */ public class FileLoggerInitializer { public static void addFileOutput(CommonOptions options_, Logger log_, String pluginName_) { - String filePathField = options_.logFilePath + "." + pluginName_; - if (!pluginName_.isEmpty()) { + String filePathField = options_.logFilePath + pluginName_ + ".log"; + if (!options_.logFilePath.isEmpty()) { try { FileHandler fileHandler = new FileHandler(filePathField, false); SimpleFormatter formatter = new SimpleFormatter(); diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java index a1618854d..10a64daff 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java @@ -83,7 +83,7 @@ public class Indexer implements AutoCloseable, IndexerService.Iface { private Indexer(Options options_) throws IOException { _options = options_; - FileLoggerInitializer.addFileOutput(_options, _log, "indexPlugin"); + FileLoggerInitializer.addFileOutput(_options, _log, "indexer"); try { _indexDir = FSDirectory.open(new File(_options.indexDirPath), diff --git a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java index f223dd07b..b9055f89d 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java @@ -1,6 +1,7 @@ package cc.search.service.app.query; import cc.search.service.app.SearchAppCommon; +import cc.search.common.FileLoggerInitializer; import cc.search.common.config.InvalidValueException; import cc.search.common.config.UnknownArgumentException; import cc.search.match.QueryContext; @@ -35,6 +36,7 @@ public class QueryApp extends SearchAppCommon { private QueryApp(QueryAppOptions options_) throws IOException { super(options_); _appOptions = options_; + FileLoggerInitializer.addFileOutput(options_, _log, "query"); } /** diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java index d73dd3e64..952f724d3 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java @@ -48,7 +48,7 @@ public static void main(String[] args_) { try { // Initialize file logger ServiceAppOptions options = new ServiceAppOptions(args_); - FileLoggerInitializer.addFileOutput(options, _log, "searchPlugin"); + FileLoggerInitializer.addFileOutput(options, _log, "search"); _log.info("Search server started!"); try (ServiceApp app = diff --git a/util/include/util/logutil.h b/util/include/util/logutil.h index aa76d1d51..9b6a6cf1a 100644 --- a/util/include/util/logutil.h +++ b/util/include/util/logutil.h @@ -12,6 +12,8 @@ namespace util void initConsoleLogger(); +std::string getLoggingBase(const std::string& path_, const std::string& name_); + bool initFileLogger(const std::string& path_); boost::log::trivial::severity_level getSeverityLevel(); diff --git a/util/src/logutil.cpp b/util/src/logutil.cpp index bf74c873b..74904e84e 100644 --- a/util/src/logutil.cpp +++ b/util/src/logutil.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace cc { @@ -78,6 +79,18 @@ void initConsoleLogger() fsSink->set_formatter(&consoleLogFormatter); } +std::string getLoggingBase(const std::string& path_, const std::string& name_) +{ + if (path_.find('~') != std::string::npos) + { + throw std::invalid_argument("The path should not contain a \'~\' character. \ + Please provide an absolute path"); + } + using namespace boost::filesystem; + + return canonical(absolute(path(path_))).string() + '/' + name_ + '_'; +} + bool initFileLogger(const std::string& path_) { auto fsSink = boost::log::add_file_log( diff --git a/webserver/src/webserver.cpp b/webserver/src/webserver.cpp index 96d8c9d24..873b158c1 100644 --- a/webserver/src/webserver.cpp +++ b/webserver/src/webserver.cpp @@ -40,7 +40,7 @@ po::options_description commandLineArguments() ("jobs,j", po::value()->default_value(4), "Number of worker threads.") ("log-target", po::value(), - "This is the path to the file where the logging output will be written. " + "This is the path to the folder where the logging output files will be written. " "If omitted, the output will be on the console only."); return desc; @@ -72,7 +72,11 @@ int main(int argc, char* argv[]) if (vm.count("log-target")) { - if(!cc::util::initFileLogger(vm["log-target"].as())) + vm.at("log-target").value() = cc::util::getLoggingBase( vm["log-target"].as() + , "CodeCompass" + ); + + if (!cc::util::initFileLogger(vm["log-target"].as() + "webserver.log")) { vm.at("log-target").value() = std::string(""); } From 83b9b1d5f50901f4641b8168df5e9773596ebc3c Mon Sep 17 00:00:00 2001 From: Tamas Dunai Date: Mon, 14 Nov 2022 23:29:54 +0100 Subject: [PATCH 5/9] Create logging directory in case it does not exist --- util/src/logutil.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/util/src/logutil.cpp b/util/src/logutil.cpp index 74904e84e..39209cded 100644 --- a/util/src/logutil.cpp +++ b/util/src/logutil.cpp @@ -1,9 +1,9 @@ #include +#include #include #include #include -#include namespace cc { @@ -86,8 +86,16 @@ std::string getLoggingBase(const std::string& path_, const std::string& name_) throw std::invalid_argument("The path should not contain a \'~\' character. \ Please provide an absolute path"); } + using namespace boost::filesystem; + boost::system::error_code ec; + create_directory(path_, ec); + if (ec) + { + throw std::invalid_argument("Permission denied to create " + path_); + } + return canonical(absolute(path(path_))).string() + '/' + name_ + '_'; } From d0bcf77a10a4e85d2517c08825d4650a1f03ca7f Mon Sep 17 00:00:00 2001 From: Tamas Dunai Date: Sat, 5 Nov 2022 23:17:38 +0100 Subject: [PATCH 6/9] Added timestamp to logs --- .../src/cc/search/indexer/app/Indexer.java | 8 +++++++ .../cc/search/service/app/query/QueryApp.java | 8 +++++++ .../service/app/service/ServiceApp.java | 8 +++++++ util/src/logutil.cpp | 22 ++++++++++++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java index 10a64daff..0b1cc67f5 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java @@ -76,6 +76,14 @@ public class Indexer implements AutoCloseable, IndexerService.Iface { */ private long _docModifiedCounter = 0; + /** + * Setup logging format + */ + static { + System.setProperty( "java.util.logging.SimpleFormatter.format" + , "%1$tY-%1$tm-%1$td %1$tT.%1$tL [%4$s] %5$s%6$s%n"); + } + /** * @param options_ command line options * @throws IOException diff --git a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java index b9055f89d..57826774e 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java @@ -28,6 +28,14 @@ public class QueryApp extends SearchAppCommon { * Application options. */ private final QueryAppOptions _appOptions; + + /** + * Setup logging format + */ + static { + System.setProperty( "java.util.logging.SimpleFormatter.format" + , "%1$tY-%1$tm-%1$td %1$tT.%1$tL [%4$s] %5$s%6$s%n"); + } /** * @param options_ command line options. diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java index 952f724d3..26003f66a 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java @@ -22,6 +22,14 @@ public class ServiceApp extends SearchHandler { */ private final IPCProcessor _processor; + /** + * Setup logging format + */ + static { + System.setProperty( "java.util.logging.SimpleFormatter.format" + , "%1$tY-%1$tm-%1$td %1$tT.%1$tL [%4$s] %5$s%6$s%n"); + } + /** * @param options_ command line options. * @throws IOException diff --git a/util/src/logutil.cpp b/util/src/logutil.cpp index 39209cded..b0c8cf6f2 100644 --- a/util/src/logutil.cpp +++ b/util/src/logutil.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace cc { @@ -13,6 +14,22 @@ namespace util namespace { +BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime) + +std::string getFormattedTime(boost::posix_time::ptime ptime_) +{ + std::stringstream stream; + boost::posix_time::time_facet* facet = new boost::posix_time::time_facet(); + facet->format("%Y-%m-%d %H:%M:%S%F"); + stream.imbue(std::locale(std::locale::classic(), facet)); + + stream << ptime_; + std::string ret = stream.str(); + ret.resize(ret.size() - 3); // keep milliseconds from microseconds + + return ret; +} + void consoleLogFormatter( const boost::log::record_view& rec, boost::log::formatting_ostream& strm) { @@ -41,7 +58,9 @@ void consoleLogFormatter( std::string sLevel = boost::log::trivial::to_string(severity.get()); std::transform(sLevel.begin(), sLevel.end(), sLevel.begin(), ::toupper); - strm << "[" << sLevel << "] " << rec[boost::log::expressions::smessage]; + std::string timeString = getFormattedTime(rec[timestamp].get()); + + strm << timeString << " [" << sLevel << "] " << rec[boost::log::expressions::smessage]; // Restore the default color if (severity) @@ -72,6 +91,7 @@ boost::log::trivial::severity_level getSeverityLevel() void initConsoleLogger() { + boost::log::add_common_attributes(); auto fsSink = boost::log::add_console_log( std::cout, boost::log::keywords::auto_flush = true); From 2ba52f74dc28622f1b6566cb0ad4c2bc6bbf9061 Mon Sep 17 00:00:00 2001 From: Tamas Dunai Date: Sun, 13 Nov 2022 22:39:31 +0100 Subject: [PATCH 7/9] Changed to nanosecond precision. Simplified java timestamp handling --- .../indexer-java/src/cc/search/indexer/app/Indexer.java | 8 -------- plugins/search/indexer/src/indexerprocess.cpp | 2 +- plugins/search/service/include/service/serviceprocess.h | 2 +- .../src/cc/search/service/app/query/QueryApp.java | 8 -------- .../src/cc/search/service/app/service/ServiceApp.java | 8 -------- util/src/logutil.cpp | 5 ++++- 6 files changed, 6 insertions(+), 27 deletions(-) diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java index 0b1cc67f5..10a64daff 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java @@ -76,14 +76,6 @@ public class Indexer implements AutoCloseable, IndexerService.Iface { */ private long _docModifiedCounter = 0; - /** - * Setup logging format - */ - static { - System.setProperty( "java.util.logging.SimpleFormatter.format" - , "%1$tY-%1$tm-%1$td %1$tT.%1$tL [%4$s] %5$s%6$s%n"); - } - /** * @param options_ command line options * @throws IOException diff --git a/plugins/search/indexer/src/indexerprocess.cpp b/plugins/search/indexer/src/indexerprocess.cpp index b1f92caef..d209e3b1b 100644 --- a/plugins/search/indexer/src/indexerprocess.cpp +++ b/plugins/search/indexer/src/indexerprocess.cpp @@ -61,7 +61,7 @@ IndexerProcess::IndexerProcess( "java", JAVAMEMORYAMOUNT, "-classpath", classpath.c_str(), "-Djava.util.logging.config.class=cc.search.common.config.LogConfigurator", - "-Djava.util.logging.SimpleFormatter.format=[%4$s] %5$s%6$s%n", + "-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tT.%1$tN [%4$s] %5$s%6$s%n", logLevelOpt.c_str(), "cc.search.indexer.app.Indexer", "-indexDB", indexDatabase_.c_str(), diff --git a/plugins/search/service/include/service/serviceprocess.h b/plugins/search/service/include/service/serviceprocess.h index 3a61d3233..53076f198 100644 --- a/plugins/search/service/include/service/serviceprocess.h +++ b/plugins/search/service/include/service/serviceprocess.h @@ -64,7 +64,7 @@ class ServiceProcess : public SearchServiceIf, public util::PipedProcess "-classpath", classpath.c_str(), //"-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8666", "-Djava.util.logging.config.class=cc.search.common.config.LogConfigurator", - "-Djava.util.logging.SimpleFormatter.format=[%4$s] %5$s%6$s%n", + "-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tT.%1$tN [%4$s] %5$s%6$s%n", logLevelOpt.c_str(), "cc.search.service.app.service.ServiceApp", "-indexDB", _indexDatabase.c_str(), diff --git a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java index 57826774e..b9055f89d 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java @@ -28,14 +28,6 @@ public class QueryApp extends SearchAppCommon { * Application options. */ private final QueryAppOptions _appOptions; - - /** - * Setup logging format - */ - static { - System.setProperty( "java.util.logging.SimpleFormatter.format" - , "%1$tY-%1$tm-%1$td %1$tT.%1$tL [%4$s] %5$s%6$s%n"); - } /** * @param options_ command line options. diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java index 26003f66a..952f724d3 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java @@ -22,14 +22,6 @@ public class ServiceApp extends SearchHandler { */ private final IPCProcessor _processor; - /** - * Setup logging format - */ - static { - System.setProperty( "java.util.logging.SimpleFormatter.format" - , "%1$tY-%1$tm-%1$td %1$tT.%1$tL [%4$s] %5$s%6$s%n"); - } - /** * @param options_ command line options. * @throws IOException diff --git a/util/src/logutil.cpp b/util/src/logutil.cpp index b0c8cf6f2..903ff3256 100644 --- a/util/src/logutil.cpp +++ b/util/src/logutil.cpp @@ -14,6 +14,9 @@ namespace util namespace { +// timestamp length required for nanosecond precision +static constexpr size_t TIMESTAMP_LENGTH = 29; + BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime) std::string getFormattedTime(boost::posix_time::ptime ptime_) @@ -25,7 +28,7 @@ std::string getFormattedTime(boost::posix_time::ptime ptime_) stream << ptime_; std::string ret = stream.str(); - ret.resize(ret.size() - 3); // keep milliseconds from microseconds + ret.resize(TIMESTAMP_LENGTH, '0'); // we need nanoseconds even if it is 0 return ret; } From 13ed8b8b7438d53f69b8a9990e3b3f3ca7853328 Mon Sep 17 00:00:00 2001 From: Tamas Dunai Date: Sun, 15 Jan 2023 01:11:35 +0100 Subject: [PATCH 8/9] Remove nanoseconds. Fix indentation. Add logging to useage.md --- doc/usage.md | 7 +++++++ plugins/search/indexer/src/indexerprocess.cpp | 2 +- plugins/search/service/include/service/serviceprocess.h | 2 +- util/src/logutil.cpp | 6 ++---- webserver/src/webserver.cpp | 8 ++++---- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/doc/usage.md b/doc/usage.md index c96d99155..1f4411c5f 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -285,3 +285,10 @@ CodeCompass_webserver \ The server will be available in a browser on [`http://localhost:6251`](http://localhost:6251). + +### Logging + +In both the parser and the webserver it is possible to write the logs to a given directory. +This feature can be enabled by passing the `--log-target` command line option with the full +path to the directory to be used for storing the log files. +If this argument is not specified, the logs will be written to the terminal only. \ No newline at end of file diff --git a/plugins/search/indexer/src/indexerprocess.cpp b/plugins/search/indexer/src/indexerprocess.cpp index d209e3b1b..c2064f62d 100644 --- a/plugins/search/indexer/src/indexerprocess.cpp +++ b/plugins/search/indexer/src/indexerprocess.cpp @@ -61,7 +61,7 @@ IndexerProcess::IndexerProcess( "java", JAVAMEMORYAMOUNT, "-classpath", classpath.c_str(), "-Djava.util.logging.config.class=cc.search.common.config.LogConfigurator", - "-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tT.%1$tN [%4$s] %5$s%6$s%n", + "-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tT [%4$s] %5$s%6$s%n", logLevelOpt.c_str(), "cc.search.indexer.app.Indexer", "-indexDB", indexDatabase_.c_str(), diff --git a/plugins/search/service/include/service/serviceprocess.h b/plugins/search/service/include/service/serviceprocess.h index 53076f198..319788aba 100644 --- a/plugins/search/service/include/service/serviceprocess.h +++ b/plugins/search/service/include/service/serviceprocess.h @@ -64,7 +64,7 @@ class ServiceProcess : public SearchServiceIf, public util::PipedProcess "-classpath", classpath.c_str(), //"-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8666", "-Djava.util.logging.config.class=cc.search.common.config.LogConfigurator", - "-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tT.%1$tN [%4$s] %5$s%6$s%n", + "-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tT [%4$s] %5$s%6$s%n", logLevelOpt.c_str(), "cc.search.service.app.service.ServiceApp", "-indexDB", _indexDatabase.c_str(), diff --git a/util/src/logutil.cpp b/util/src/logutil.cpp index 903ff3256..f94addfa5 100644 --- a/util/src/logutil.cpp +++ b/util/src/logutil.cpp @@ -23,14 +23,12 @@ std::string getFormattedTime(boost::posix_time::ptime ptime_) { std::stringstream stream; boost::posix_time::time_facet* facet = new boost::posix_time::time_facet(); - facet->format("%Y-%m-%d %H:%M:%S%F"); + facet->format("%Y-%m-%d %H:%M:%S"); stream.imbue(std::locale(std::locale::classic(), facet)); stream << ptime_; - std::string ret = stream.str(); - ret.resize(TIMESTAMP_LENGTH, '0'); // we need nanoseconds even if it is 0 - return ret; + return stream.str(); } void consoleLogFormatter( diff --git a/webserver/src/webserver.cpp b/webserver/src/webserver.cpp index 873b158c1..c2413c489 100644 --- a/webserver/src/webserver.cpp +++ b/webserver/src/webserver.cpp @@ -76,10 +76,10 @@ int main(int argc, char* argv[]) , "CodeCompass" ); - if (!cc::util::initFileLogger(vm["log-target"].as() + "webserver.log")) - { - vm.at("log-target").value() = std::string(""); - } + if (!cc::util::initFileLogger(vm["log-target"].as() + "webserver.log")) + { + vm.at("log-target").value() = std::string(""); + } } if (argc < 2 || vm.count("help")) From 0565e90c1cd931ff4daf718a48d5c803a33adc3b Mon Sep 17 00:00:00 2001 From: tamasdunai Date: Sun, 12 Feb 2023 16:34:44 +0100 Subject: [PATCH 9/9] Renamings, reorderings, small fixes --- doc/usage.md | 2 +- parser/src/parser.cpp | 16 ++++++++-------- .../search/common/config/LogConfigurator.java | 2 +- .../src/cc/search/common/ipc/IPCProcessor.java | 2 +- .../src/cc/search/analysis/SourceAnalyzer.java | 2 +- .../src/cc/search/analysis/tags/CTags.java | 2 +- .../analysis/tags/SourceTagGenerator.java | 2 +- .../analysis/tags/TagGeneratorManager.java | 2 +- .../src/cc/search/indexer/AbstractIndexer.java | 2 +- .../src/cc/search/indexer/FileIndexer.java | 2 +- .../src/cc/search/indexer/app/Indexer.java | 2 +- .../cc/search/suggestion/DatabaseBuilder.java | 2 +- plugins/search/parser/src/searchparser.cpp | 4 ++-- .../src/cc/search/analysis/QueryAnalyzer.java | 2 +- .../src/cc/search/analysis/query/HIPDQuery.java | 2 +- .../match/matcher/MasterMatcherFactory.java | 2 +- .../match/matcher/TagKindMatcherFactory.java | 2 +- .../cc/search/service/app/SearchAppCommon.java | 2 +- .../cc/search/service/app/query/QueryApp.java | 2 +- .../service/app/service/SearchHandler.java | 2 +- .../search/service/app/service/ServiceApp.java | 2 +- .../cc/search/suggestion/SuggestionHandler.java | 2 +- plugins/search/service/src/searchservice.cpp | 4 ++-- util/src/logutil.cpp | 17 +++++++++-------- webserver/src/webserver.cpp | 16 ++++++++-------- 25 files changed, 49 insertions(+), 48 deletions(-) diff --git a/doc/usage.md b/doc/usage.md index 1f4411c5f..ab31e4105 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -289,6 +289,6 @@ The server will be available in a browser on ### Logging In both the parser and the webserver it is possible to write the logs to a given directory. -This feature can be enabled by passing the `--log-target` command line option with the full +This feature can be enabled by passing the `--logtarget` command line option with the full path to the directory to be used for storing the log files. If this argument is not specified, the logs will be written to the terminal only. \ No newline at end of file diff --git a/parser/src/parser.cpp b/parser/src/parser.cpp index 700ec73c4..63ffad303 100644 --- a/parser/src/parser.cpp +++ b/parser/src/parser.cpp @@ -70,6 +70,9 @@ po::options_description commandLineArguments() po::value()->default_value(trivial::info), "Logging legel of the parser. Possible values are: debug, info, warning, " "error, critical.") + ("logtarget", po::value(), + "This is the path to the folder where the logging output files will be written. " + "If omitted, the output will be on the console only.") ("jobs,j", po::value()->default_value(4), "Number of threads the parsers can use.") ("skip,s", po::value>(), @@ -82,10 +85,7 @@ po::options_description commandLineArguments() "further actions modifying the state of the database.") ("incremental-threshold", po::value()->default_value(10), "This is a threshold percentage. If the total ratio of changed files " - "is greater than this value, full parse is forced instead of incremental parsing.") - ("log-target", po::value(), - "This is the path to the folder where the logging output files will be written. " - "If omitted, the output will be on the console only."); + "is greater than this value, full parse is forced instead of incremental parsing."); return desc; } @@ -232,14 +232,14 @@ int main(int argc, char* argv[]) po::store(po::command_line_parser(argc, argv) .options(desc).allow_unregistered().run(), vm); - if (vm.count("log-target")) + if (vm.count("logtarget")) { - vm.at("log-target").value() = cc::util::getLoggingBase( vm["log-target"].as() + vm.at("logtarget").value() = cc::util::getLoggingBase( vm["logtarget"].as() , vm["name"].as() ); - if (!cc::util::initFileLogger(vm["log-target"].as() + "parser.log")) + if (!cc::util::initFileLogger(vm["logtarget"].as() + "parser.log")) { - vm.at("log-target").value() = std::string(""); + vm.at("logtarget").value() = std::string(); } } diff --git a/plugins/search/common/src/cc/search/common/config/LogConfigurator.java b/plugins/search/common/src/cc/search/common/config/LogConfigurator.java index f56e7e600..44b86c79c 100644 --- a/plugins/search/common/src/cc/search/common/config/LogConfigurator.java +++ b/plugins/search/common/src/cc/search/common/config/LogConfigurator.java @@ -25,7 +25,7 @@ public LogConfigurator() { logManager.readConfiguration(cfgStream); } catch (IOException | SecurityException ex) { - Logger.getLogger("GLOBAL_LOGGER").log(Level.SEVERE, + Logger.getGlobal().log(Level.SEVERE, "Failed to configure custom log propeties!", ex); } } diff --git a/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java b/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java index 782d6aebd..d4f37c8e0 100644 --- a/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java +++ b/plugins/search/common/src/cc/search/common/ipc/IPCProcessor.java @@ -22,7 +22,7 @@ public class IPCProcessor implements AutoCloseable { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Processor for thrift messages. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java b/plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java index 573b53d18..5d8c5ff1a 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/analysis/SourceAnalyzer.java @@ -20,7 +20,7 @@ public final class SourceAnalyzer extends AnalyzerWrapper { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Analyzer for a full path. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java index f0b0dc2d7..b9d56240c 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/CTags.java @@ -19,7 +19,7 @@ class CTags implements TagGenerator { /** * Logger. */ - private final static Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private final static Logger _log = Logger.getGlobal(); /** * Filter terminator string for ctags. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/SourceTagGenerator.java b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/SourceTagGenerator.java index b8ff6aedc..f178e53da 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/SourceTagGenerator.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/SourceTagGenerator.java @@ -21,7 +21,7 @@ public class SourceTagGenerator implements TagGenerator { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Options array for initializing _artf460600CTags. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/TagGeneratorManager.java b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/TagGeneratorManager.java index c5452298f..eeb7def1a 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/TagGeneratorManager.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/analysis/tags/TagGeneratorManager.java @@ -14,7 +14,7 @@ public final class TagGeneratorManager implements AutoCloseable { /** * Logger. */ - private final static Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private final static Logger _log = Logger.getGlobal(); /** * Singleton instance. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/AbstractIndexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/AbstractIndexer.java index 05eb0b81e..3ff1d8e6e 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/AbstractIndexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/AbstractIndexer.java @@ -42,7 +42,7 @@ abstract public class AbstractIndexer { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Field type for storing tags. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/FileIndexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/FileIndexer.java index e1b56fcc2..58fa8cfe7 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/FileIndexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/FileIndexer.java @@ -16,7 +16,7 @@ public class FileIndexer extends AbstractIndexer { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * File path to index. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java index 10a64daff..fb34ce0c5 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/indexer/app/Indexer.java @@ -41,7 +41,7 @@ public class Indexer implements AutoCloseable, IndexerService.Iface { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Command line options. */ diff --git a/plugins/search/indexer/indexer-java/src/cc/search/suggestion/DatabaseBuilder.java b/plugins/search/indexer/indexer-java/src/cc/search/suggestion/DatabaseBuilder.java index cb3d7d2c9..583245943 100644 --- a/plugins/search/indexer/indexer-java/src/cc/search/suggestion/DatabaseBuilder.java +++ b/plugins/search/indexer/indexer-java/src/cc/search/suggestion/DatabaseBuilder.java @@ -27,7 +27,7 @@ public final class DatabaseBuilder { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * An index reader for the main index database. */ diff --git a/plugins/search/parser/src/searchparser.cpp b/plugins/search/parser/src/searchparser.cpp index 62a025eb0..9306e04cd 100644 --- a/plugins/search/parser/src/searchparser.cpp +++ b/plugins/search/parser/src/searchparser.cpp @@ -72,8 +72,8 @@ SearchParser::SearchParser(ParserContext& ctx_) : AbstractParser(ctx_), ctx_.compassRoot, IndexerProcess::OpenMode::Create, IndexerProcess::LockMode::Simple, - ctx_.options.count("log-target") - ? ctx_.options["log-target"].as() + ctx_.options.count("logtarget") + ? ctx_.options["logtarget"].as() : "")); } catch (const IndexerProcess::Failure& ex_) diff --git a/plugins/search/service/search-java/src/cc/search/analysis/QueryAnalyzer.java b/plugins/search/service/search-java/src/cc/search/analysis/QueryAnalyzer.java index 6ea24bd6e..480bfb139 100644 --- a/plugins/search/service/search-java/src/cc/search/analysis/QueryAnalyzer.java +++ b/plugins/search/service/search-java/src/cc/search/analysis/QueryAnalyzer.java @@ -17,7 +17,7 @@ public class QueryAnalyzer extends AnalyzerWrapper { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * A simple analyzer for queries. */ diff --git a/plugins/search/service/search-java/src/cc/search/analysis/query/HIPDQuery.java b/plugins/search/service/search-java/src/cc/search/analysis/query/HIPDQuery.java index 074381f13..d0ecce586 100644 --- a/plugins/search/service/search-java/src/cc/search/analysis/query/HIPDQuery.java +++ b/plugins/search/service/search-java/src/cc/search/analysis/query/HIPDQuery.java @@ -50,7 +50,7 @@ public class HIPDQuery extends Query { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * The default value of the maximal allowed difference. */ diff --git a/plugins/search/service/search-java/src/cc/search/match/matcher/MasterMatcherFactory.java b/plugins/search/service/search-java/src/cc/search/match/matcher/MasterMatcherFactory.java index b48f0f601..bf96f3127 100644 --- a/plugins/search/service/search-java/src/cc/search/match/matcher/MasterMatcherFactory.java +++ b/plugins/search/service/search-java/src/cc/search/match/matcher/MasterMatcherFactory.java @@ -15,7 +15,7 @@ public class MasterMatcherFactory implements ResultMatcherFactory { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Matcher wrapper for matching with multiple matchers. */ diff --git a/plugins/search/service/search-java/src/cc/search/match/matcher/TagKindMatcherFactory.java b/plugins/search/service/search-java/src/cc/search/match/matcher/TagKindMatcherFactory.java index 924ab5022..0aab1f2a8 100644 --- a/plugins/search/service/search-java/src/cc/search/match/matcher/TagKindMatcherFactory.java +++ b/plugins/search/service/search-java/src/cc/search/match/matcher/TagKindMatcherFactory.java @@ -22,7 +22,7 @@ class TagKindMatcherFactory implements ResultMatcherFactory { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Token filter for filtering relevant tokens by its kind. */ diff --git a/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java b/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java index cef069905..39d49ef86 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/SearchAppCommon.java @@ -75,7 +75,7 @@ public abstract class SearchAppCommon implements AutoCloseable { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Async task class for matching lines in a document. diff --git a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java index b9055f89d..68fc2ef3b 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/query/QueryApp.java @@ -23,7 +23,7 @@ public class QueryApp extends SearchAppCommon { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Application options. */ diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java b/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java index 3e929de87..383ef9618 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/SearchHandler.java @@ -49,7 +49,7 @@ abstract class SearchHandler extends SearchAppCommon /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Handler for suggestion requests. */ diff --git a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java index 952f724d3..e47ff6523 100644 --- a/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java +++ b/plugins/search/service/search-java/src/cc/search/service/app/service/ServiceApp.java @@ -16,7 +16,7 @@ public class ServiceApp extends SearchHandler { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * IPC message processor. */ diff --git a/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java b/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java index 8a1b7aaf6..45bde166d 100644 --- a/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java +++ b/plugins/search/service/search-java/src/cc/search/suggestion/SuggestionHandler.java @@ -25,7 +25,7 @@ public final class SuggestionHandler implements AutoCloseable { /** * Logger. */ - private static final Logger _log = Logger.getLogger("GLOBAL_LOGGER"); + private static final Logger _log = Logger.getGlobal(); /** * Filename suggester. */ diff --git a/plugins/search/service/src/searchservice.cpp b/plugins/search/service/src/searchservice.cpp index e2dd9d1ae..a3ae32551 100644 --- a/plugins/search/service/src/searchservice.cpp +++ b/plugins/search/service/src/searchservice.cpp @@ -107,8 +107,8 @@ SearchServiceHandler::SearchServiceHandler( { _javaProcess.reset(new ServiceProcess(*datadir_ + "/search", context_.compassRoot, - context_.options.count("log-target") - ? context_.options["log-target"].as() + context_.options.count("logtarget") + ? context_.options["logtarget"].as() : "")); } diff --git a/util/src/logutil.cpp b/util/src/logutil.cpp index f94addfa5..60c7ac9ff 100644 --- a/util/src/logutil.cpp +++ b/util/src/logutil.cpp @@ -6,6 +6,8 @@ #include #include +namespace fs = boost::filesystem; + namespace cc { namespace util @@ -14,9 +16,6 @@ namespace util namespace { -// timestamp length required for nanosecond precision -static constexpr size_t TIMESTAMP_LENGTH = 29; - BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime) std::string getFormattedTime(boost::posix_time::ptime ptime_) @@ -78,7 +77,9 @@ void fileLogFormatter( std::string sLevel = boost::log::trivial::to_string(severity.get()); std::transform(sLevel.begin(), sLevel.end(), sLevel.begin(), ::toupper); - strm << "[" << sLevel << "] " << rec[boost::log::expressions::smessage]; + std::string timeString = getFormattedTime(rec[timestamp].get()); + + strm << timeString << " [" << sLevel << "] " << rec[boost::log::expressions::smessage]; } } // namespace @@ -108,16 +109,16 @@ std::string getLoggingBase(const std::string& path_, const std::string& name_) Please provide an absolute path"); } - using namespace boost::filesystem; - boost::system::error_code ec; - create_directory(path_, ec); + fs::create_directory(path_, ec); if (ec) { throw std::invalid_argument("Permission denied to create " + path_); } - return canonical(absolute(path(path_))).string() + '/' + name_ + '_'; + const std::string fullpath = canonical(absolute(fs::path(path_))).string(); + + return fullpath + (fullpath.back() == '/' ? "" : "/") + name_ + '_'; } bool initFileLogger(const std::string& path_) diff --git a/webserver/src/webserver.cpp b/webserver/src/webserver.cpp index c2413c489..a6deabaf8 100644 --- a/webserver/src/webserver.cpp +++ b/webserver/src/webserver.cpp @@ -37,11 +37,11 @@ po::options_description commandLineArguments() po::value()->default_value(trivial::info), "Logging level of the parser. Possible values are: debug, info, warning, " "error, critical") - ("jobs,j", po::value()->default_value(4), - "Number of worker threads.") - ("log-target", po::value(), + ("logtarget", po::value(), "This is the path to the folder where the logging output files will be written. " - "If omitted, the output will be on the console only."); + "If omitted, the output will be on the console only.") + ("jobs,j", po::value()->default_value(4), + "Number of worker threads."); return desc; } @@ -70,15 +70,15 @@ int main(int argc, char* argv[]) po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); - if (vm.count("log-target")) + if (vm.count("logtarget")) { - vm.at("log-target").value() = cc::util::getLoggingBase( vm["log-target"].as() + vm.at("logtarget").value() = cc::util::getLoggingBase( vm["logtarget"].as() , "CodeCompass" ); - if (!cc::util::initFileLogger(vm["log-target"].as() + "webserver.log")) + if (!cc::util::initFileLogger(vm["logtarget"].as() + "webserver.log")) { - vm.at("log-target").value() = std::string(""); + vm.at("logtarget").value() = std::string(); } }