Skip to content

Commit

Permalink
Add console grep-like utility
Browse files Browse the repository at this point in the history
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
variar committed May 27, 2021
1 parent 6736068 commit c16f41d
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 127 deletions.
32 changes: 22 additions & 10 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,34 @@ add_custom_command (OUTPUT ${DOCUMENTATION_HTML}
DEPENDS maddy ${DOCUMENTATION_FILE})

set(MAIN_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/crashhandler.h
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cli.h
${CMAKE_CURRENT_SOURCE_DIR}/klogg.qrc
)

set(KLOGG_UI_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/messagereceiver.h
${CMAKE_CURRENT_SOURCE_DIR}/crashhandler.h
${CMAKE_CURRENT_SOURCE_DIR}/kloggapp.h
${CMAKE_CURRENT_SOURCE_DIR}/klogg.qrc
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
${DOCUMENTATION_RESOURCE}
${ICON_FILE})

set(KLOGG_GREP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/klogg_grep.cpp)


set(MAIN_LIBS ui cli11 singleapp)
set(MAIN_LIBS logdata cli11 singleapp)
if(KLOGG_OVERRIDE_MALLOC)
LIST(PREPEND MAIN_LIBS klogg_malloc_proxy)
if(KLOGG_USE_MIMALLOC AND NOT WIN32)
LIST(PREPEND MAIN_SOURCES $<TARGET_OBJECTS:mimalloc-obj>)
endif()
endif()

add_executable(klogg ${OS_BUNDLE} ${MAIN_SOURCES})
add_executable(klogg_portable ${OS_BUNDLE} ${MAIN_SOURCES})
add_executable(klogg ${OS_BUNDLE} ${MAIN_SOURCES} ${KLOGG_UI_SOURCES})
add_executable(klogg_portable ${OS_BUNDLE} ${MAIN_SOURCES} ${KLOGG_UI_SOURCES})
add_executable(klogg_grep ${MAIN_SOURCES} ${KLOGG_GREP_SOURCES} )

add_dependencies(ci_build klogg)
add_dependencies(ci_build klogg klogg_grep)
if (WIN32)
add_dependencies(ci_build klogg_portable)
endif()
Expand All @@ -45,21 +51,27 @@ set_target_properties(klogg PROPERTIES AUTORCC ON)
set_target_properties(klogg PROPERTIES AUTOMOC ON)
set_target_properties(klogg_portable PROPERTIES AUTORCC ON)
set_target_properties(klogg_portable PROPERTIES AUTOMOC ON)
set_target_properties(klogg_grep PROPERTIES AUTORCC ON)
set_target_properties(klogg_grep PROPERTIES AUTOMOC ON)

if (${KLOGG_USE_LTO})
set_property(TARGET klogg PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
set_property(TARGET klogg_portable PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
set_property(TARGET klogg_grep PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)

endif()

target_link_libraries(klogg PUBLIC ${MAIN_LIBS})
target_link_libraries(klogg_portable PUBLIC ${MAIN_LIBS})
target_link_libraries(klogg PUBLIC ${MAIN_LIBS} ui)
target_link_libraries(klogg_portable PUBLIC ${MAIN_LIBS} ui)
target_link_libraries(klogg_grep PUBLIC ${MAIN_LIBS})

target_compile_definitions(klogg_portable PUBLIC -DKLOGG_PORTABLE)

if (WIN32)

target_sources(klogg PRIVATE ${ProductVersionResourceFiles})
target_sources(klogg_portable PRIVATE ${ProductVersionResourceFiles})
target_sources(klogg_grep PRIVATE ${ProductVersionResourceFiles})

elseif (APPLE)
set_source_files_properties(${ICON_FILE} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
Expand Down
169 changes: 169 additions & 0 deletions src/app/cli.h
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
88 changes: 88 additions & 0 deletions src/app/klogg_grep.cpp
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();
}
Loading

0 comments on commit c16f41d

Please sign in to comment.