Skip to content

Commit 33e4ec1

Browse files
committed
add command to clear logs
1 parent 48c8e61 commit 33e4ec1

File tree

7 files changed

+51
-36
lines changed

7 files changed

+51
-36
lines changed

Diff for: README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ To install Sysgrab, follow these steps:
2020

2121
Add the extracted directory to your system PATH to make `sysgrab` accessible from anywhere in the terminal.
2222

23-
Add this line to your shell configuration to make the change persistent
23+
Add this line to your shell configuration to make the change persistent:
2424

2525
```bash
2626
export PATH=$PATH:/path/to/sysgrab-directory
@@ -39,15 +39,16 @@ sysgrab [OPTIONS]
3939
(no option) Display system information
4040
-h, --help Show a help message and exit
4141
-v, --version Display version information and exit
42+
-d, --delete-logs Delete logs
4243
```
4344

4445
## Configuration
4546

4647
To configure Sysgrab, follow these steps:
4748

48-
1. **Configure colors, logging, and data ordering**:
49+
1. **Configure colors and data ordering**:
4950

50-
To configure the colors, edit the `config.yaml` file.
51+
To configure the colors and ordering, edit the `config.yaml` file.
5152

5253
2. **Add art**:
5354

Diff for: include/sysgrab_config.h

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ typedef struct Color {
1010
} Color;
1111

1212
typedef struct Config {
13-
bool log_errors;
1413
Color base_color;
1514
Color accent_color;
1615
char *art_file_name;

Diff for: include/sysgrab_log.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#define SYSGRAB_LOGS_H
33

44
char *get_log_file_path(void);
5+
bool delete_logs(void);
56

67
#endif

Diff for: resources/config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
log_errors: True
21
art_file_name: art.txt
32
base_color: [255, 255, 255]
43
accent_color: [20, 200, 255]

Diff for: src/main.c

+18-19
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ int main(int argc, char *argv[]) {
1818

1919
static struct option long_options[] = {{"help", no_argument, 0, 'h'},
2020
{"version", no_argument, 0, 'v'},
21+
{"delete-logs", no_argument, 0, 'd'},
2122
{0, 0, 0, 0}};
2223

23-
while ((opt = getopt_long(argc, argv, "hv", long_options, &option_index)) !=
24+
while ((opt = getopt_long(argc, argv, "hvd", long_options, &option_index)) !=
2425
-1) {
2526
switch (opt) {
2627
case 'h':
@@ -29,6 +30,13 @@ int main(int argc, char *argv[]) {
2930
case 'v':
3031
printf("Version %s\n", VERSION);
3132
exit(EXIT_SUCCESS);
33+
case 'd':
34+
if (!delete_logs()) {
35+
printf("Failed to delete logs\n");
36+
} else {
37+
printf("Successfully deleted logs\n");
38+
}
39+
exit(EXIT_SUCCESS);
3240
case '?':
3341
if (optopt) {
3442
fprintf(stderr, "Invalid option: -%c\n", optopt);
@@ -40,6 +48,15 @@ int main(int argc, char *argv[]) {
4048
}
4149
}
4250

51+
const char *log_file_path = get_log_file_path();
52+
53+
FILE *log_fp = NULL;
54+
log_fp = freopen(log_file_path, "w", stderr);
55+
if (log_fp == NULL) {
56+
perror("Failed to redirect stderr");
57+
exit(EXIT_FAILURE);
58+
}
59+
4360
char *config_path = get_file_path(CONFIG_FILE_NAME);
4461
if (config_path == NULL) {
4562
perror("Failed to get config file path");
@@ -52,24 +69,6 @@ int main(int argc, char *argv[]) {
5269
exit(EXIT_FAILURE);
5370
}
5471

55-
FILE *log_fp = NULL;
56-
57-
if (config->log_errors) {
58-
const char *log_file_path = get_log_file_path();
59-
60-
log_fp = freopen(log_file_path, "w", stderr);
61-
if (log_fp == NULL) {
62-
perror("Failed to redirect stderr");
63-
exit(EXIT_FAILURE);
64-
}
65-
} else {
66-
log_fp = freopen("/dev/null", "w", stderr);
67-
if (log_fp == NULL) {
68-
perror("Failed to redirect stderr");
69-
exit(EXIT_FAILURE);
70-
}
71-
}
72-
7372
char *art_path = get_file_path(config->art_file_name);
7473

7574
Art *art = get_art(art_path);

Diff for: src/sysgrab_config.c

+5-11
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
#define MAX_LEN 256
1313

14-
Config *create_config(bool log_errors, Color base_color, Color accent_color,
15-
char *art_file_name, Data *data) {
14+
Config *create_config(Color base_color, Color accent_color, char *art_file_name,
15+
Data *data) {
1616
Config *config = malloc(sizeof(Config));
1717
if (config == NULL) {
1818
perror("Failed to allocate memory");
1919
return NULL;
2020
}
2121

22-
config->log_errors = log_errors;
2322
config->base_color = base_color;
2423
config->accent_color = accent_color;
2524
config->art_file_name = art_file_name;
@@ -37,8 +36,7 @@ FILE *open_config_file(const char *file_path) {
3736
return NULL;
3837
}
3938

40-
const char *default_yaml = "log_errors: True\n"
41-
"art_file_name: art.txt\n"
39+
const char *default_yaml = "art_file_name: art.txt\n"
4240
"base_color: [255, 255, 255]\n"
4341
"accent_color: [20, 200, 255]\n"
4442
"ordering:\n"
@@ -80,7 +78,6 @@ Config *get_config(const char *file_path) {
8078

8179
yaml_parser_set_input_file(&parser, fp);
8280

83-
bool log_errors = true;
8481
Color base_color;
8582
Color accent_color;
8683
char *art_file_name = NULL;
@@ -117,9 +114,7 @@ Config *get_config(const char *file_path) {
117114
if (strcmp(current_key, "") == 0) {
118115
strncpy(current_key, value, sizeof(current_key) - 1);
119116
} else {
120-
if (strcmp(current_key, "log_errors") == 0) {
121-
log_errors = (strcmp(value, "True") == 0);
122-
} else if (strcmp(current_key, "art_file_name") == 0) {
117+
if (strcmp(current_key, "art_file_name") == 0) {
123118
art_file_name = strdup(value);
124119
if (art_file_name == NULL) {
125120
perror("Failed to create string");
@@ -182,8 +177,7 @@ Config *get_config(const char *file_path) {
182177
yaml_parser_delete(&parser);
183178
fclose(fp);
184179

185-
config =
186-
create_config(log_errors, base_color, accent_color, art_file_name, data);
180+
config = create_config(base_color, accent_color, art_file_name, data);
187181
if (config == NULL) {
188182
perror("Failed to create config");
189183
return NULL;

Diff for: src/sysgrab_log.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#include "sysgrab_path.h"
99
#include "sysgrab_time.h"
1010

11+
#define LOG_FILE_DIRECTORY "logs"
1112
#define MAX_LEN 1024
1213

1314
char *get_log_file_directory_path(void) {
14-
char *path = get_file_path("logs");
15+
char *path = get_file_path(LOG_FILE_DIRECTORY);
1516
if (path == NULL) {
1617
perror("Failed to get exectable directory path");
1718
return NULL;
@@ -47,3 +48,24 @@ char *get_log_file_path(void) {
4748

4849
return path;
4950
}
51+
52+
bool delete_logs(void) {
53+
char *log_file_directory_path = get_log_file_directory_path();
54+
if (log_file_directory_path == NULL) {
55+
perror("Failed to get log file directory path");
56+
return false;
57+
}
58+
59+
char temp[1024];
60+
snprintf(temp, sizeof(temp), "rm -rf \"%s\"", log_file_directory_path);
61+
62+
int status = system(temp);
63+
if (status != 0) {
64+
perror("Failed to delete logs");
65+
return false;
66+
}
67+
68+
free(log_file_directory_path);
69+
70+
return true;
71+
}

0 commit comments

Comments
 (0)