mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-12-26 12:27:52 +00:00
tailhead: convert to generic object payload
This commit is contained in:
parent
1d73d82b87
commit
f6942c8c98
@ -1439,10 +1439,7 @@ void free_text_objects(struct text_object *root, int internal)
|
|||||||
break;
|
break;
|
||||||
case OBJ_head:
|
case OBJ_head:
|
||||||
case OBJ_tail:
|
case OBJ_tail:
|
||||||
free(data.headtail.logfile);
|
free_tailhead(obj);
|
||||||
if(data.headtail.buffer) {
|
|
||||||
free(data.headtail.buffer);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case OBJ_text:
|
case OBJ_text:
|
||||||
case OBJ_font:
|
case OBJ_font:
|
||||||
|
103
src/tailhead.c
103
src/tailhead.c
@ -37,7 +37,16 @@
|
|||||||
#define MAX_HEADTAIL_LINES 30
|
#define MAX_HEADTAIL_LINES 30
|
||||||
#define DEFAULT_MAX_HEADTAIL_USES 2
|
#define DEFAULT_MAX_HEADTAIL_USES 2
|
||||||
|
|
||||||
void tailstring(char *string, int endofstring, int wantedlines) {
|
struct headtail {
|
||||||
|
int wantedlines;
|
||||||
|
char *logfile;
|
||||||
|
char *buffer;
|
||||||
|
int current_use;
|
||||||
|
int max_uses;
|
||||||
|
int reported;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void tailstring(char *string, int endofstring, int wantedlines) {
|
||||||
int i, linescounted = 0;
|
int i, linescounted = 0;
|
||||||
|
|
||||||
string[endofstring] = 0;
|
string[endofstring] = 0;
|
||||||
@ -56,53 +65,77 @@ void tailstring(char *string, int endofstring, int wantedlines) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_tailhead(struct text_object *obj)
|
||||||
|
{
|
||||||
|
struct headtail *ht = obj->data.opaque;
|
||||||
|
if (!ht)
|
||||||
|
return;
|
||||||
|
if (ht->logfile)
|
||||||
|
free(ht->logfile);
|
||||||
|
if (ht->buffer)
|
||||||
|
free(ht->buffer);
|
||||||
|
free(obj->data.opaque);
|
||||||
|
obj->data.opaque = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
|
void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) {
|
||||||
unsigned int args;
|
unsigned int args;
|
||||||
|
struct headtail *ht;
|
||||||
|
|
||||||
obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE);
|
ht = malloc(sizeof(struct headtail));
|
||||||
obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES;
|
memset(ht, 0, sizeof(struct headtail));
|
||||||
args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses);
|
|
||||||
if(args == 2 || args == 3) {
|
ht->logfile = malloc(DEFAULT_TEXT_BUFFER_SIZE);
|
||||||
if(obj->data.headtail.max_uses < 1) {
|
memset(ht->logfile, 0, DEFAULT_TEXT_BUFFER_SIZE);
|
||||||
free(obj->data.headtail.logfile);
|
|
||||||
CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
|
ht->max_uses = DEFAULT_MAX_HEADTAIL_USES;
|
||||||
}
|
|
||||||
if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) {
|
args = sscanf(arg, "%s %d %d", ht->logfile, &ht->wantedlines, &ht->max_uses);
|
||||||
to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile);
|
if (args < 2 || args > 3) {
|
||||||
obj->data.headtail.buffer = NULL;
|
free_tailhead(obj);
|
||||||
obj->data.headtail.current_use = 0;
|
|
||||||
}else{
|
|
||||||
free(obj->data.headtail.logfile);
|
|
||||||
CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
free(obj->data.headtail.logfile);
|
|
||||||
CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
|
CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type);
|
||||||
}
|
}
|
||||||
|
if (ht->max_uses < 1) {
|
||||||
|
free_tailhead(obj);
|
||||||
|
CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type);
|
||||||
|
}
|
||||||
|
if (ht->wantedlines > 0 && ht->wantedlines <= MAX_HEADTAIL_LINES) {
|
||||||
|
to_real_path(ht->logfile, ht->logfile);
|
||||||
|
ht->buffer = NULL;
|
||||||
|
ht->current_use = 0;
|
||||||
|
} else {
|
||||||
|
free_tailhead(obj);
|
||||||
|
CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES);
|
||||||
|
}
|
||||||
|
obj->data.opaque = ht;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
|
void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size) {
|
||||||
int fd, i, endofstring = 0, linescounted = 0;
|
int fd, i, endofstring = 0, linescounted = 0;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
struct headtail *ht = obj->data.opaque;
|
||||||
|
|
||||||
|
if (!ht)
|
||||||
|
return;
|
||||||
|
|
||||||
//empty the buffer and reset the counter if we used it the max number of times
|
//empty the buffer and reset the counter if we used it the max number of times
|
||||||
if(obj->data.headtail.buffer && obj->data.headtail.current_use >= obj->data.headtail.max_uses - 1) {
|
if(ht->buffer && ht->current_use >= ht->max_uses - 1) {
|
||||||
free(obj->data.headtail.buffer);
|
free(ht->buffer);
|
||||||
obj->data.headtail.buffer = NULL;
|
ht->buffer = NULL;
|
||||||
obj->data.headtail.current_use = 0;
|
ht->current_use = 0;
|
||||||
}
|
}
|
||||||
//use the buffer if possible
|
//use the buffer if possible
|
||||||
if(obj->data.headtail.buffer) {
|
if(ht->buffer) {
|
||||||
strcpy(p, obj->data.headtail.buffer);
|
strcpy(p, ht->buffer);
|
||||||
obj->data.headtail.current_use++;
|
ht->current_use++;
|
||||||
}else{ //otherwise find the needed data
|
}else{ //otherwise find the needed data
|
||||||
if(stat(obj->data.headtail.logfile, &st) == 0) {
|
if(stat(ht->logfile, &st) == 0) {
|
||||||
if (S_ISFIFO(st.st_mode)) {
|
if (S_ISFIFO(st.st_mode)) {
|
||||||
fd = open_fifo(obj->data.headtail.logfile, &obj->a);
|
fd = open_fifo(ht->logfile, &ht->reported);
|
||||||
if(fd != -1) {
|
if(fd != -1) {
|
||||||
if(strcmp(type, "head") == 0) {
|
if(strcmp(type, "head") == 0) {
|
||||||
for(i = 0; linescounted < obj->data.headtail.wantedlines; i++) {
|
for(i = 0; linescounted < ht->wantedlines; i++) {
|
||||||
read(fd, p + i, 1);
|
read(fd, p + i, 1);
|
||||||
if(p[i] == '\n') {
|
if(p[i] == '\n') {
|
||||||
linescounted++;
|
linescounted++;
|
||||||
@ -111,33 +144,33 @@ void print_tailhead(const char* type, struct text_object *obj, char *p, int p_ma
|
|||||||
p[i] = 0;
|
p[i] = 0;
|
||||||
} else if(strcmp(type, "tail") == 0) {
|
} else if(strcmp(type, "tail") == 0) {
|
||||||
i = read(fd, p, p_max_size - 1);
|
i = read(fd, p, p_max_size - 1);
|
||||||
tailstring(p, i, obj->data.headtail.wantedlines);
|
tailstring(p, i, ht->wantedlines);
|
||||||
} else {
|
} else {
|
||||||
CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
|
CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
} else {
|
} else {
|
||||||
fp = open_file(obj->data.headtail.logfile, &obj->a);
|
fp = open_file(ht->logfile, &ht->reported);
|
||||||
if(fp != NULL) {
|
if(fp != NULL) {
|
||||||
if(strcmp(type, "head") == 0) {
|
if(strcmp(type, "head") == 0) {
|
||||||
for(i = 0; i < obj->data.headtail.wantedlines; i++) {
|
for(i = 0; i < ht->wantedlines; i++) {
|
||||||
fgets(p + endofstring, p_max_size - endofstring, fp);
|
fgets(p + endofstring, p_max_size - endofstring, fp);
|
||||||
endofstring = strlen(p);
|
endofstring = strlen(p);
|
||||||
}
|
}
|
||||||
} else if(strcmp(type, "tail") == 0) {
|
} else if(strcmp(type, "tail") == 0) {
|
||||||
fseek(fp, - p_max_size, SEEK_END);
|
fseek(fp, - p_max_size, SEEK_END);
|
||||||
i = fread(p, 1, p_max_size - 1, fp);
|
i = fread(p, 1, p_max_size - 1, fp);
|
||||||
tailstring(p, i, obj->data.headtail.wantedlines);
|
tailstring(p, i, ht->wantedlines);
|
||||||
} else {
|
} else {
|
||||||
CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
|
CRIT_ERR(NULL, NULL, "If you are seeing this then there is a bug in the code, report it !");
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
obj->data.headtail.buffer = strdup(p);
|
ht->buffer = strdup(p);
|
||||||
} else {
|
} else {
|
||||||
CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, obj->data.headtail.logfile);
|
CRIT_ERR(NULL, NULL, "$%s can't find information about %s", type, ht->logfile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -27,5 +27,11 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash);
|
#ifndef _TAILHEAD_H
|
||||||
void print_tailhead(const char* type, struct text_object *obj, char *p, int p_max_size);
|
#define _TAILHEAD_H
|
||||||
|
|
||||||
|
void free_tailhead(struct text_object *);
|
||||||
|
void init_tailhead(const char *, const char *, struct text_object *, void *);
|
||||||
|
void print_tailhead(const char *, struct text_object *, char *, int);
|
||||||
|
|
||||||
|
#endif /* _TAILHEAD_H */
|
||||||
|
@ -489,14 +489,6 @@ struct text_object {
|
|||||||
char *s;
|
char *s;
|
||||||
} top;
|
} top;
|
||||||
|
|
||||||
struct {
|
|
||||||
int wantedlines;
|
|
||||||
char *logfile;
|
|
||||||
char *buffer;
|
|
||||||
int current_use;
|
|
||||||
int max_uses;
|
|
||||||
} headtail;
|
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
double last_update;
|
double last_update;
|
||||||
float interval;
|
float interval;
|
||||||
|
Loading…
Reference in New Issue
Block a user