Created
July 18, 2020 09:25
-
-
Save arashrasoulzadeh/e05512e0388f562db9824d63d86e06c2 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <regex> | |
void processFirstLine(char *string); | |
using namespace std; | |
const std::regex ERROR_FILENAME_REGEX("\\w+\\.php on line \\d+"); | |
const std::regex ERROR_LINE_REGEX("\\[\\d+-\\d+-\\d+ \\d+:\\d+:\\d+\\]", | |
std::regex_constants::ECMAScript | std::regex_constants::icase); | |
const char *FILENAME = larave_log_location; | |
int main() { | |
FILE *fp = fopen(FILENAME, "r"); | |
if (fp == NULL) | |
exit(EXIT_FAILURE); | |
char *line = NULL; | |
size_t len = 0; | |
while ((getline(&line, &len, fp)) != -1) { | |
if (std::regex_search(line, ERROR_LINE_REGEX)) { | |
// found match | |
processFirstLine(line); | |
} | |
} | |
fclose(fp); | |
if (line) | |
free(line); | |
} | |
void processFirstLine(char *string) { | |
std::string filenames(string); | |
std::string result; | |
try { | |
std::smatch match; | |
if (std::regex_search(filenames, match, ERROR_FILENAME_REGEX) && match.size() > 0) { | |
result = match.str(1); | |
cout << match.str(0) << "\n"; | |
} else { | |
result = std::string(""); | |
} | |
} catch (std::regex_error &e) { | |
// Syntax error in the regular expression | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment