Skip to content

Commit

Permalink
A new logging system that's compliant with multi-threading and Window…
Browse files Browse the repository at this point in the history
…s filename convention
  • Loading branch information
Vocelia committed Jul 3, 2023
1 parent 7bbc6e6 commit ddcf563
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
101 changes: 55 additions & 46 deletions src/exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,73 @@ static short backslash = 0;

int execute_command(const char* cmd) {
time(&now);
char cmdname[214];
char filename[256];
char ctime_plhdr[26];
unsigned long cmd_len;
char cmd_out[strlen(cmd)+4];
char* output = new char[512];
char cmd_buff[strlen(cmd)+5];
char filepath[strlen(LOGS_PATH)+256];
sprintf(cmd_buff, "%s 2>&1", cmd);
#ifdef _WIN32
FILE* pfd = _popen(cmd_buff, "r");
#else
FILE* pfd = popen(cmd_buff, "r");
#endif
char* output = new char[512];
char** cmd_spl = strspl(cmd, ' ', &cmd_len);
#ifdef _WIN32
const char* tkn = strrchr(cmd_spl[0], '/');
if (tkn) { tkn++; snprintf(cmdname, 214, "%s", tkn); }
else {
tkn = strrchr(cmd_spl[0], '\\');
if (tkn) { tkn++; snprintf(cmdname, 214, "%s", tkn); backslash = 1; }
else snprintf(cmdname, 214, "%s", cmd_spl[0]);
}
#else
const char* tkn = strrchr(cmd_spl[0], '/');
if (tkn) { tkn++; snprintf(cmdname, 214, "%s", tkn); }
else snprintf(cmdname, 214, "%s", cmd_spl[0]);
#endif
strcpy(ctime_plhdr, ctime(&now));
ctime_plhdr[strlen(ctime(&now))-1] = '\0';
sprintf(filename, "%s-%lu-[%s].txt", ctime_plhdr, clock(), cmdname); //214 + 42 = 256
if (!backslash) sprintf(filepath, "%s/%s", LOGS_PATH, filename);
else sprintf(filepath, "%s\\%s", LOGS_PATH, filename);
//[Phase]: Error Handling
if (!pfd) {
printf("[ERR]: Failed to read from process \"%s\"!\n", cmd);
return -1;
}
switch (COMMAND_OUTPUT) {
case 0: break;
case 1:
while (fgets(output, sizeof(output), pfd) != NULL) printf("%s", output);
break;
case 2:
write_file(filepath, "a", ctime(&now), strlen(ctime(&now)));
sprintf(cmd_out, "[%s]\n", cmd);
write_file(filepath, "a", cmd_out, strlen(cmd_out));
write_file(filepath, "a", "-------------------------------------------------\n", 50);
while (fgets(output, 512, pfd) != NULL) {
unsigned long output_len = strlen(output);
if (output[output_len-1] == '\n') output[output_len] = '\0';
write_file(filepath, "a", output, output_len);
//Logging has a reserved section to avoid unnecessary computational complexity
if (COMMAND_OUTPUT==2) {
char cmdname[214];
char filename[256];
char ctime_plhdr[26];
unsigned long cmd_len;
char cmd_out[strlen(cmd)+4];
char filepath[strlen(LOGS_PATH)+256];
//[Phase]: Preparing a valid filepath
char** cmd_spl = strspl(cmd, ' ', &cmd_len);
#ifdef _WIN32
const char* tkn = strrchr(cmd_spl[0], '/');
if (tkn) { tkn++; snprintf(cmdname, 214, "%s", tkn); }
else {
tkn = strrchr(cmd_spl[0], '\\');
if (tkn) { tkn++; snprintf(cmdname, 214, "%s", tkn); backslash = 1; }
else snprintf(cmdname, 214, "%s", cmd_spl[0]);
}
write_file(filepath, "a", "-------------------------------------------------\n\n", 51);
break;
default:
printf("[ERR]: Unavailable option: \"%d\" in command_output value!\n", COMMAND_OUTPUT);
return -1;
break;
#else
const char* tkn = strrchr(cmd_spl[0], '/');
if (tkn) { tkn++; snprintf(cmdname, 214, "%s", tkn); }
else snprintf(cmdname, 214, "%s", cmd_spl[0]);
#endif
strcpy(ctime_plhdr, ctime(&now));
ctime_plhdr[strlen(ctime(&now))-1] = '\0';
#ifdef _WIN32
strrplc(ctime_plhdr, ':', '-'); //Windows filename compatibility
#endif
sprintf(filename, "[%s]#%s#%lu.txt", cmdname, ctime_plhdr, clock());
//214: Command name + 42: Date, Clock, and the rest of the string = 256: Filename limit
if (!backslash) sprintf(filepath, "%s/%s", LOGS_PATH, filename);
else sprintf(filepath, "%s\\%s", LOGS_PATH, filename);
//[Phase]: Writing to the given filepath
write_file(filepath, "a", ctime(&now), strlen(ctime(&now)));
sprintf(cmd_out, "[%s]\n", cmd);
write_file(filepath, "a", cmd_out, strlen(cmd_out));
write_file(filepath, "a", "-------------------------------------------------\n", 50);
while (fgets(output, 512, pfd) != NULL) {
unsigned long output_len = strlen(output);
if (output[output_len-1] == '\n') output[output_len] = '\0';
write_file(filepath, "a", output, output_len);
}
write_file(filepath, "a", "-------------------------------------------------\n", 50);
} else {
switch (COMMAND_OUTPUT) {
case 0: break;
case 1:
while (fgets(output, sizeof(output), pfd) != NULL) printf("%s", output);
break;
default:
printf("[ERR]: Unavailable option: \"%d\" in command_output value!\n", COMMAND_OUTPUT);
return -1;
break;
}
}
delete[] output;
#ifdef _WIN32
Expand Down
1 change: 0 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ int main(int argc, char** argv) {
printf("[ERR]: The path provided isn't valid!\n");
return -1;
}
//delete[] LOGS_PATH_RW;
printf("LOGS_PATH is a valid directory!\n");
printf("Reading from config.ini...\n");
CONFIG = new Config("./data/config.ini");
Expand Down

0 comments on commit ddcf563

Please sign in to comment.