forked from nickbnf/glogg
-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commits adds a klogg_grep console tool (#306). It uses klogg search engine to find all matches for given pattern in a file. It is intended to be used for acceptance testing. It can't replace grep-like utilities because klogg_grep builds an index of given file and only after that does pattern matching. Indexing beforehand is great if one needs to scan for several patterns, but completely unnecessary for single search in a file.
- Loading branch information
Showing
4 changed files
with
284 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright (C) 2021 Anton Filimonov and other contributors | ||
* | ||
* This file is part of klogg. | ||
* | ||
* klogg is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* klogg is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with klogg. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef KLOGG_CLI_H | ||
#define KLOGG_CLI_H | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
#include <QCommandLineParser> | ||
#include <QCoreApplication> | ||
#include <QFileInfo> | ||
#include <QString> | ||
|
||
#include "klogg_version.h" | ||
#include "log.h" | ||
|
||
struct CliParameters { | ||
bool new_session = false; | ||
bool load_session = false; | ||
bool multi_instance = false; | ||
bool log_to_file = false; | ||
bool follow_file = false; | ||
int64_t log_level = static_cast<int64_t>( plog::warning ); | ||
|
||
std::vector<QString> filenames; | ||
|
||
int window_width = 0; | ||
int window_height = 0; | ||
|
||
QString pattern; | ||
|
||
CliParameters( QCoreApplication& app, bool console = false ) | ||
{ | ||
QCommandLineParser parser; | ||
parser.setApplicationDescription( "Klogg log viewer" ); | ||
const auto helpOption = parser.addHelpOption(); | ||
const auto versionOption = parser.addVersionOption(); | ||
|
||
const QCommandLineOption multiInstanceOption( | ||
QStringList() << "m" | ||
<< "multi", | ||
"allow multiple instance of klogg to run simultaneously (use together with -s)" ); | ||
|
||
const QCommandLineOption loadSessionOption( | ||
QStringList() << "s" | ||
<< "load-session", | ||
"load the previous session (default when no file is passed)" ); | ||
|
||
const QCommandLineOption newSessionOption( | ||
QStringList() << "n" | ||
<< "new-session", | ||
"do not load the previous session (default when a file is passed)" ); | ||
|
||
const QCommandLineOption logToFileOption( QStringList() << "l" | ||
<< "log", | ||
"save the log to a file" ); | ||
|
||
const QCommandLineOption followOption( QStringList() << "f" | ||
<< "follow", | ||
"follow initial opened files" ); | ||
|
||
const QCommandLineOption windowWidthOption( "window-width", "new window width", "1024" ); | ||
|
||
const QCommandLineOption windowHeightOption( "window-height", "new window height", "768" ); | ||
|
||
const QCommandLineOption patternOption( QStringList() << "e" | ||
<< "pattern", | ||
"pattern to search for", "pattern" ); | ||
|
||
const QCommandLineOption debugOption( | ||
QStringList() << "d" | ||
<< "debug", | ||
"output more debug (increase number for more verbosity)", "debug_level", "0" ); | ||
|
||
parser.addOption( debugOption ); | ||
|
||
if ( !console ) { | ||
parser.addOption( multiInstanceOption ); | ||
parser.addOption( loadSessionOption ); | ||
parser.addOption( newSessionOption ); | ||
parser.addOption( logToFileOption ); | ||
parser.addOption( followOption ); | ||
parser.addOption( windowWidthOption ); | ||
parser.addOption( windowHeightOption ); | ||
} | ||
else { | ||
parser.addOption( patternOption ); | ||
} | ||
|
||
parser.process( app ); | ||
|
||
if ( parser.isSet( helpOption ) ) { | ||
parser.showHelp( EXIT_SUCCESS ); | ||
} | ||
|
||
if ( parser.isSet( versionOption ) ) { | ||
print_version(); | ||
exit( EXIT_SUCCESS ); | ||
} | ||
|
||
log_level = static_cast<int64_t>( plog::warning ) + parser.value( debugOption ).toInt(); | ||
|
||
if ( !console ) { | ||
if ( parser.isSet( multiInstanceOption ) ) { | ||
multi_instance = true; | ||
} | ||
|
||
if ( parser.isSet( loadSessionOption ) ) { | ||
load_session = true; | ||
} | ||
|
||
if ( parser.isSet( newSessionOption ) ) { | ||
new_session = true; | ||
} | ||
|
||
if ( parser.isSet( logToFileOption ) ) { | ||
log_to_file = true; | ||
} | ||
|
||
if ( parser.isSet( followOption ) ) { | ||
follow_file = true; | ||
} | ||
} | ||
else { | ||
|
||
if ( parser.isSet( patternOption ) ) { | ||
pattern = parser.value( patternOption ); | ||
} | ||
} | ||
|
||
for ( const auto& file : parser.positionalArguments() ) { | ||
const auto fileInfo = QFileInfo( file ); | ||
filenames.emplace_back( fileInfo.absoluteFilePath() ); | ||
} | ||
} | ||
|
||
static void print_version() | ||
{ | ||
std::cout << "klogg " << kloggVersion().data() << "\n"; | ||
std::cout << "Built " << kloggBuildDate().data() << " from " << kloggCommit().data() << "(" | ||
<< kloggGitVersion().data() << ")\n"; | ||
|
||
std::cout | ||
<< "Copyright (C) 2020 Nicolas Bonnefon, Anton Filimonov and other contributors\n"; | ||
std::cout | ||
<< "This is free software. You may redistribute copies of it under the terms of\n"; | ||
std::cout << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"; | ||
std::cout << "There is NO WARRANTY, to the extent permitted by law.\n"; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (C) 2021 Anton Filimonov and other contributors | ||
* | ||
* This file is part of klogg. | ||
* | ||
* klogg is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* klogg is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with klogg. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <plog/Appenders/ColorConsoleAppender.h> | ||
|
||
#if defined( KLOGG_USE_TBBMALLOC ) | ||
#include <tbb/tbbmalloc_proxy.h> | ||
#elif defined( KLOGG_USE_MIMALLOC ) | ||
#include <mimalloc.h> | ||
#endif | ||
|
||
#include "configuration.h" | ||
#include "data/logdata.h" | ||
#include "data/logfiltereddata.h" | ||
#include "dispatch_to.h" | ||
#include "log.h" | ||
#include "persistentinfo.h" | ||
|
||
#include "cli.h" | ||
|
||
const bool PersistentInfo::ForcePortable = true; | ||
|
||
int main( int argc, char* argv[] ) | ||
{ | ||
#ifdef KLOGG_USE_MIMALLOC | ||
mi_stats_reset(); | ||
#endif | ||
qRegisterMetaType<LinesCount>( "LinesCount" ); | ||
qRegisterMetaType<LineNumber>( "LineNumber" ); | ||
|
||
QCoreApplication app( argc, argv ); | ||
CliParameters parameters( app, true ); | ||
|
||
auto logAppender = std::make_unique<plog::ColorConsoleAppender<plog::GloggFormatter>>(); | ||
|
||
plog::init( static_cast<plog::Severity>( parameters.log_level ), logAppender.get() ); | ||
|
||
auto configuration = Configuration::getSynced(); | ||
|
||
LogData logData; | ||
auto filteredData = logData.getNewFilteredData(); | ||
|
||
filteredData->connect( | ||
filteredData.get(), &LogFilteredData::searchProgressed, | ||
[ & ]( LinesCount nbMatches, int progress, LineNumber ) { | ||
if ( progress == 100 ) { | ||
|
||
LOG_INFO << "Searched finished, got " << nbMatches.get() << " matches"; | ||
|
||
const auto defaultChunkSize = 1000_lcount; | ||
for ( auto chunkStart = 0_lnum; chunkStart < nbMatches; | ||
chunkStart = chunkStart + defaultChunkSize ) { | ||
auto chunkSize | ||
= std::min( defaultChunkSize.get(), nbMatches.get() - chunkStart.get() ); | ||
auto lines = filteredData->getLines( chunkStart, LinesCount( chunkSize ) ); | ||
for ( const auto& l : lines ) { | ||
std::cout << l.toStdString() << "\n"; | ||
} | ||
} | ||
|
||
exit( EXIT_SUCCESS ); | ||
} | ||
} ); | ||
|
||
logData.connect( &logData, &LogData::loadingFinished, [ & ]() { | ||
dispatchToMainThread( | ||
[ & ] { filteredData->runSearch( RegularExpressionPattern( parameters.pattern ) ); } ); | ||
} ); | ||
|
||
logData.attachFile( parameters.filenames.front() ); | ||
return app.exec(); | ||
} |
Oops, something went wrong.