first version

This commit is contained in:
Gero Müller 2016-02-08 10:52:14 +01:00
parent 2dad6de1e8
commit 85a656f417
3 changed files with 143 additions and 0 deletions

1
.gitignore vendored
View File

@ -62,3 +62,4 @@
# Debug files
*.dSYM/
build

10
CMakeLists.txt Normal file
View File

@ -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)

132
main.cpp Normal file
View File

@ -0,0 +1,132 @@
#include <cstdio>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <systemd/sd-journal.h>
#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;
}