$head, mem leak fixes
authorBrenden Matthews <brenden1@rty.ca>
Fri, 26 Aug 2005 05:52:43 +0000 (05:52 +0000)
committerBrenden Matthews <brenden1@rty.ca>
Fri, 26 Aug 2005 05:52:43 +0000 (05:52 +0000)
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@191 7f574dfc-610e-0410-a909-a81674777703

ChangeLog
README
doc/variables.xml
src/conky.c
src/x11.c

index 5368111..6215e09 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,7 @@
        * applied $freq fixes patch, adds new $freq_dyn (sf.net patch 1271633)
        * removed units from $freq* output
        * fix for graph config parsing
+       * Added $head, fixed mem leak with $tail
 
 2005-08-24
        * More configure and makefile updates
diff --git a/README b/README
index bfa79fb..2418847 100644 (file)
--- a/README
+++ b/README
@@ -457,6 +457,12 @@ VARIABLES
              File system used space
 
 
+       head logfile lines (interval)
+             Displays  first  N lines of supplied text text file. If interval
+             is not supplied, Conky assumes 2x Conky's interval.  Max  of  30
+             lines can be displayed, or until the text buffer is filled.
+
+
        hr (height)
              Horizontal line, height is the height in pixels
 
@@ -655,9 +661,10 @@ VARIABLES
              Move text over by N pixels. See also $voffset.
 
 
-       tail   logfile, lines interval> Displays last N lines of supplied  text
-             text file. If interval is not supplied, Conky assumes 2x Conky's
-             interval. Max of 30 lines. Max of 30 lines can be displayed.
+       tail logfile lines (interval)
+             Displays last N lines of supplied text text file. If interval is
+             not supplied, Conky assumes 2x Conky's interval. Max of 30 lines
+             can be displayed, or until the text buffer is filled.
 
 
        time (format)
index 38a6e42..e6960f6 100644 (file)
 
 <varlistentry>
        <term>
+               <command><option>head</option></command>
+               <option>logfile lines (interval)</option>
+       </term>
+       <listitem>
+               Displays first N lines of supplied text text file. If interval is not supplied, Conky assumes 2x Conky's interval. Max of 30 lines can be displayed, or until the text buffer is filled.
+               <para></para></listitem>
+</varlistentry>
+
+<varlistentry>
+       <term>
                <command><option>hr</option></command>
        <option>(height)</option>
        </term>
 <varlistentry>
        <term>
                <command><option>tail</option></command>
+               <option>logfile lines (interval)</option>
        </term>
        <listitem>
-               logfile, lines interval> Displays last N lines of supplied text text file. If interval is not supplied, Conky assumes 2x Conky's interval. Max of 30 lines. Max of 30 lines can be displayed.
+               Displays last N lines of supplied text text file. If interval is not supplied, Conky assumes 2x Conky's interval. Max of 30 lines can be displayed, or until the text buffer is filled.
        <para></para></listitem>
 </varlistentry>
 
index 8190ff0..a6572d7 100644 (file)
@@ -678,9 +678,9 @@ static void convert_escapes(char *buf)
 /* converts from bytes to human readable format (k, M, G, T) */
 static void human_readable(long long a, char *buf, int size)
 {
-       //Strange conditional due to possible overflows
+       // Strange conditional due to possible overflows
        if(a / 1024 / 1024 / 1024.0 > 1024.0){
-               snprintf(buf, size, "%.2fT", (a / 1024 / 1024) / 1024 / 1024.0);
+               snprintf(buf, size, "%.2fT", (a / 1024 / 1024 / 1024) / 1024.0);
        }
        else if (a >= 1024 * 1024 * 1024) {
                snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
@@ -752,6 +752,7 @@ enum text_object_type {
        OBJ_top,
        OBJ_top_mem,
        OBJ_tail,
+       OBJ_head,
        OBJ_kernel,
        OBJ_loadavg,
        OBJ_machine,
@@ -1304,7 +1305,67 @@ if (s[0] == '#') {
                ERR("invalid args given for tail");
                return;
        }
-       obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 6); /* asumming all else worked */
+       obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
+       END OBJ(head, 0)
+                       char buf[64];
+       int n1, n2;
+       if (!arg) {
+               ERR("head needs arguments");
+               obj->type = OBJ_text;
+               obj->data.s = strdup("${head}");
+               return;
+       }
+       if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
+               if (n1 < 1 || n1 > 30) {
+                       CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
+                       return;
+               } else {
+                       FILE *fp;
+                       fp = fopen(buf, "rt");
+                       if (fp != NULL) {
+                               obj->data.tail.logfile =
+                                               malloc(TEXT_BUFFER_SIZE);
+                               strcpy(obj->data.tail.logfile, buf);
+                               obj->data.tail.wantedlines = n1 - 1;
+                               obj->data.tail.interval =
+                                               update_interval * 2;
+                               fclose(fp);
+                       } else {
+                               //fclose (fp);
+                               CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
+                       }
+               }
+       } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
+               if (n1 < 1 || n1 > 30) {
+                       CRIT_ERR
+                                       ("invalid arg for head, number of lines must be between 1 and 30");
+                       return;
+               } else if (n2 < 1 || n2 < update_interval) {
+                       CRIT_ERR
+                                       ("invalid arg for head, interval must be greater than 0 and Conky's interval");
+                       return;
+               } else {
+                       FILE *fp;
+                       fp = fopen(buf, "rt");
+                       if (fp != NULL) {
+                               obj->data.tail.logfile =
+                                               malloc(TEXT_BUFFER_SIZE);
+                               strcpy(obj->data.tail.logfile, buf);
+                               obj->data.tail.wantedlines = n1 - 1;
+                               obj->data.tail.interval = n2;
+                               fclose(fp);
+                       } else {
+                               //fclose (fp);
+                               CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
+                       }
+               }
+       }
+
+       else {
+               ERR("invalid args given for head");
+               return;
+       }
+       obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
        END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
        if (arg) {
                r = sscanf(arg, "%d %d %d", &a, &b, &c);
@@ -1714,13 +1775,30 @@ static void generate_text()
                        }
 #endif /* X11 */
                        OBJ(diskio) {
-                               if (diskio_value > 1024) {
-                                       snprintf(p, n, "%.1fM",
-                                                (double)diskio_value/1024);
-                               } else if (diskio_value > 0) {
-                                       snprintf(p, n, "%dK", diskio_value);
+                               if (!use_spacer) {
+                                       if (diskio_value > 1024*1024) {
+                                               snprintf(p, n, "%.1fG",
+                                                               (double)diskio_value/1024);
+                                       } else if (diskio_value > 1024) {
+                                               snprintf(p, n, "%.1fM",
+                                                               (double)diskio_value/1024);
+                                       } else if (diskio_value > 0) {
+                                               snprintf(p, n, "%dK", diskio_value);
+                                       } else {
+                                               snprintf(p, n, "%d", diskio_value);
+                                       }
                                } else {
-                                       snprintf(p, n, "%d", diskio_value);
+                                       if (diskio_value > 1024*1024) {
+                                               snprintf(p, 6, "%.1fG   ",
+                                                               (double)diskio_value/1024/1024);
+                                       } else if (diskio_value > 1024) {
+                                               snprintf(p, 6, "%.1fM   ",
+                                                               (double)diskio_value/1024);
+                                       } else if (diskio_value > 0) {
+                                               snprintf(p, 6, "%dK ", diskio_value);
+                                       } else {
+                                               snprintf(p, 6, "%d     ", diskio_value);
+                                       }
                                }
                        }
                        OBJ(diskiograph) {
@@ -2548,6 +2626,7 @@ static void generate_text()
                                        int added = 0;
                                        tailstring *head = NULL;
                                        tailstring *headtmp = NULL;
+                                       tailstring *freetmp = NULL;
                                        fp = fopen(obj->data.tail.logfile, "rt");
                                        if (fp == NULL) {
                                                ERR("tail logfile failed to open");
@@ -2567,17 +2646,20 @@ static void generate_text()
                                                }
 
                                                fclose(fp);
+                                               freetmp = head;
 
                                                if (obj->data.tail.readlines > 0) {
                                                        for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
                                                                addtail(&headtmp, head->data);
                                                                head = head->next;
                                                        }
+                                                       freetail(freetmp);
+                                                       freetmp = headtmp;
                                                        strcpy(obj->data.tail.buffer, headtmp->data);
                                                        headtmp = headtmp->next;
                                                        for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
                                                                if (headtmp) {
-                                                                       strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 6 / obj->data.tail.wantedlines) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
+                                                                       strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20 / obj->data.tail.wantedlines) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
                                                                        headtmp = headtmp->next;
                                                                }
                                                        }
@@ -2588,18 +2670,63 @@ static void generate_text()
                                                        }
                                                        snprintf(p, n, "%s", obj->data.tail.buffer);
 
-                                                       freetail(headtmp);
+                                                       freetail(freetmp);
+                                               }
+                                               else {
+                                                       strcpy(obj->data.tail.buffer, "Logfile Empty");
+                                                       snprintf(p, n, "Logfile Empty");
+                                               }
+                                       }
+                               }
+                       }
+                       OBJ(head) {
+                               if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
+                                       snprintf(p, n, "%s", obj->data.tail.buffer);
+                               } else {
+                                       obj->data.tail.last_update = current_update_time;
+                                       FILE *fp;
+                                       tailstring *head = NULL;
+                                       tailstring *headtmp = NULL;
+                                       tailstring *freetmp = NULL;
+                                       fp = fopen(obj->data.tail.logfile, "rt");
+                                       if (fp == NULL) {
+                                               ERR("head logfile failed to open");
+                                       }
+                                       else {
+                                               obj->data.tail.readlines = 0;
+                                               while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*4, fp) != NULL && obj->data.tail.readlines <= obj->data.tail.wantedlines) {
+                                                       addtail(&head, obj->data.tail.buffer);
+                                                       obj->data.tail.readlines++;
+                                               }
+                                               fclose(fp);
+                                               freetmp = head;
+                                               if (obj->data.tail.readlines > 0) {
+                                                       while (head) {
+                                                               addtail(&headtmp, head->data);
+                                                               head = head->next;
+                                                       }
+                                                       freetail(freetmp);
+                                                       freetmp = headtmp;
+                                                       strcpy(obj->data.tail.buffer, headtmp->data);
+                                                       headtmp = headtmp->next;
+                                                       while (headtmp) {
+                                                               strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20 / obj->data.tail.wantedlines) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
+                                                               headtmp = headtmp->next;
+                                                       }
+                                                       freetail(freetmp);
+                                                       /* get rid of any ugly newlines at the end */
+                                                       if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
+                                                               obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
+                                                       }
+                                                       snprintf(p, n, "%s", obj->data.tail.buffer);
                                                }
                                                else {
                                                        strcpy(obj->data.tail.buffer, "Logfile Empty");
                                                        snprintf(p, n, "Logfile Empty");
                                                }
-                                               freetail(head);
                                        }
                                }
                        }
-
-
 
                        break;
                }
index 40aa64c..14f8397 100644 (file)
--- a/src/x11.c
+++ b/src/x11.c
@@ -164,7 +164,7 @@ inline void set_transparent_background(Window win)
                XSetWindowBackground(display, win, background_colour);
                colour_set = background_colour;
        }
-       //XClearWindow(display, win);
+       //XClearWindow(display, win); not sure why this was here
 }
 
 #if defined OWN_WINDOW