From 85a656f4170359d646587544a55efe5963889554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gero=20M=C3=BCller?= Date: Mon, 8 Feb 2016 10:52:14 +0100 Subject: [PATCH] first version --- .gitignore | 1 + CMakeLists.txt | 10 ++++ main.cpp | 132 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore index 35fc333..4d09e18 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ # Debug files *.dSYM/ +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1460840 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 2.6) +project(journey) + +find_package(PkgConfig) +pkg_check_modules(LIBSYSTEMD REQUIRED libsystemd) +pkg_check_modules(RE2 REQUIRED re2) + +add_executable(journey main.cpp) +target_link_libraries(journey ${LIBSYSTEMD_LIBRARIES} ${RE2_LIBRARIES}) +install(TARGETS journey RUNTIME DESTINATION bin) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..33363e6 --- /dev/null +++ b/main.cpp @@ -0,0 +1,132 @@ +#include +#include +#include + +#include +#include + +#include +#include "re2/re2.h" + +using namespace re2; +using namespace std; + +// ideas: sort by usage +// configurable regex engine: http://sljit.sourceforge.net/regex_perf.html + +void save_cursor(sd_journal* j, const char* filename) { + char* cursor; + //sd_journal_seek_tail(j); + int r = sd_journal_get_cursor(j, &cursor); + + if (r >= 0) { + cout << cursor << endl; + FILE* f = fopen(filename, "wb"); + + if (f) { + fwrite(cursor, sizeof(char), strlen(cursor), f); + free(cursor); + } + + fclose(f); + } else { + cerr << "Failed to get cursor: " << strerror(-r) << endl; + } + +} + +void seek_cursor(sd_journal* j, const char* filename) { + FILE* f = fopen(filename, "rb"); + + if (f) { + fseek(f, 0, SEEK_END); + long int s = ftell(f); + + if (s > 0) { + fseek(f, 0, SEEK_SET); + char* c = (char*)malloc(s); + + size_t r = fread(c, s, 1, f); + if (r == 1) { + cout << c << endl; + sd_journal_seek_cursor(j, c); + } else { + cerr << "only " << r << endl; + } + + free(c); + } else { + cerr << "empty file" << endl; + } + + fclose(f); + } else { + fprintf(stderr, "Failed to open load cursor\n"); + } +} + +int main(int argc, char* argv[]) { + + // read rules + ifstream in("test.rules"); + string line; + RE2 re1("^\\w\\{3\\} \\[ :0-9\\]\\{11\\} \\[._\\[:alnum:\\]-\\]\\+ (.*)\\[\\[0-9\\]+\\]: (.*)$"); + + while (getline(in, line).good()) { + //cout << line << endl; + } + + + + int r; + sd_journal* j; + + r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); + + if (r < 0) { + fprintf(stderr, "Failed to open journal: %s\n", strerror(-r)); + return 1; + } + + seek_cursor(j, "last"); + // skip current + sd_journal_next(j); + + RE2 re(".*pixmap$"); + + if (!re.ok()) { + printf("pattern error!\n"); + return 1; + } + + + while (sd_journal_next(j) > 0) { + const char* d; + size_t l; + + r = sd_journal_get_data(j, "MESSAGE", (const void**)&d, &l); + + if (r < 0) { + fprintf(stderr, "Failed to read message field: %s\n", strerror(-r)); + continue; + + } + + re2::StringPiece input(d, l); + + //if (RE2::FullMatch(input, re)) { + // printf("match: %d %d @%d\n", result.data() - subject, result.size(), input.data() - subject); + printf("%.*s\n", (int) l, d); + //} + + // + } + + //sd_journal_previous(j); + save_cursor(j, "last"); + + sd_journal_close(j); + + return 0; +} +