"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.4/src/debug.c" (20 Nov 2019, 11822 Bytes) of package /linux/misc/tin-2.4.4.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "debug.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.3_vs_2.4.4.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : debug.c
    4  *  Author    : I. Lea
    5  *  Created   : 1991-04-01
    6  *  Updated   : 2019-07-23
    7  *  Notes     : debug routines
    8  *
    9  * Copyright (c) 1991-2020 Iain Lea <iain@bricbrac.de>
   10  * All rights reserved.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  *
   16  * 1. Redistributions of source code must retain the above copyright notice,
   17  *    this list of conditions and the following disclaimer.
   18  *
   19  * 2. Redistributions in binary form must reproduce the above copyright
   20  *    notice, this list of conditions and the following disclaimer in the
   21  *    documentation and/or other materials provided with the distribution.
   22  *
   23  * 3. Neither the name of the copyright holder nor the names of its
   24  *    contributors may be used to endorse or promote products derived from
   25  *    this software without specific prior written permission.
   26  *
   27  * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 
   41 #ifndef TIN_H
   42 #   include "tin.h"
   43 #endif /* !TIN_H */
   44 
   45 #ifdef DEBUG
   46 #   ifndef NEWSRC_H
   47 #       include "newsrc.h"
   48 #   endif /* !NEWSRC_H */
   49 #endif /* DEBUG */
   50 
   51 unsigned short debug;
   52 
   53 #ifdef DEBUG
   54 /*
   55  * Local prototypes
   56  */
   57 static void debug_print_attributes(struct t_attribute *attr, FILE *fp);
   58 static void debug_print_filter(FILE *fp, int num, struct t_filter *the_filter);
   59 static void debug_print_newsrc(struct t_newsrc *lnewsrc, FILE *fp);
   60 
   61 
   62 /*
   63  * remove debug files
   64  */
   65 void
   66 debug_delete_files(
   67     void)
   68 {
   69     char file[PATH_LEN];
   70 
   71     if (debug & (DEBUG_NNTP | DEBUG_REMOVE)) {
   72         joinpath(file, sizeof(file), TMPDIR, "NNTP");
   73         unlink(file);
   74     }
   75 
   76     if (debug & (DEBUG_FILTER | DEBUG_REMOVE)) {
   77         joinpath(file, sizeof(file), TMPDIR, "ARTS");
   78         unlink(file);
   79         joinpath(file, sizeof(file), TMPDIR, "FILTER");
   80         unlink(file);
   81     }
   82 
   83     if (debug & (DEBUG_NEWSRC | DEBUG_REMOVE)) {
   84         joinpath(file, sizeof(file), TMPDIR, "BITMAP");
   85         unlink(file);
   86     }
   87 
   88     if (debug & (DEBUG_REFS | DEBUG_REMOVE)) {
   89         joinpath(file, sizeof(file), TMPDIR, "REFS.dump");
   90         unlink(file);
   91         joinpath(file, sizeof(file), TMPDIR, "REFS.info");
   92         unlink(file);
   93     }
   94 
   95     if (debug & (DEBUG_MEM | DEBUG_REMOVE)) {
   96         joinpath(file, sizeof(file), TMPDIR, "MALLOC");
   97         unlink(file);
   98     }
   99 
  100     if (debug & (DEBUG_ATTRIB | DEBUG_REMOVE)) {
  101         joinpath(file, sizeof(file), TMPDIR, "ATTRIBUTES");
  102         unlink(file);
  103         joinpath(file, sizeof(file), TMPDIR, "SCOPES-R");
  104         unlink(file);
  105         joinpath(file, sizeof(file), TMPDIR, "SCOPES-W");
  106         unlink(file);
  107     }
  108 
  109     if (debug & (DEBUG_MISC | DEBUG_REMOVE)) {
  110         joinpath(file, sizeof(file), TMPDIR, "ACTIVE");
  111         unlink(file);
  112     }
  113 }
  114 
  115 
  116 /*
  117  * tin specific debug routines
  118  */
  119 void
  120 debug_print_arts(
  121     void)
  122 {
  123     int i;
  124 
  125     if (!(debug & DEBUG_FILTER))
  126         return;
  127 
  128     for_each_art(i)
  129         debug_print_header(&arts[i]);
  130 }
  131 
  132 
  133 void
  134 debug_print_header(
  135     struct t_article *s)
  136 {
  137     FILE *fp;
  138     char file[PATH_LEN];
  139 
  140     if (!(debug & DEBUG_FILTER))
  141         return;
  142 
  143     joinpath(file, sizeof(file), TMPDIR, "ARTS");
  144 
  145     if ((fp = fopen(file, "a")) != NULL) {
  146         fprintf(fp,"art=[%5"T_ARTNUM_PFMT"] tag=[%s] kill=[%s] selected=[%s]\n", s->artnum,
  147             bool_unparse(s->tagged),
  148             bool_unparse(s->killed),
  149             bool_unparse(s->selected));
  150         fprintf(fp,"subj=[%-38s]\n", s->subject);
  151         fprintf(fp,"date=[%ld]  from=[%s]  name=[%s]\n", (long) s->date, s->from,
  152             BlankIfNull(s->name));
  153         fprintf(fp,"msgid=[%s]  refs=[%s]\n",
  154             BlankIfNull(s->msgid),
  155             BlankIfNull(s->refs));
  156 
  157         if (s->killed)
  158             fprintf(fp,"score=[%d] gnksa=[%d] lines=[%d]\n", s->score, s->gnksa_code, s->line_count);
  159 
  160         if (s->archive) {
  161             fprintf(fp, "archive.name=[%-38s]  ", s->archive->name);
  162             if (s->archive->partnum)
  163                 fprintf(fp, "archive.partnum=[%s]  ", s->archive->partnum);
  164             if (s->archive->ispart)
  165                 fprintf(fp, "archive.ispart=[%s]\n", bool_unparse(s->archive->ispart));
  166         }
  167         fprintf(fp,"thread=[%d]  prev=[%d]  status=[%u]\n\n", s->thread, s->prev, s->status);
  168         fflush(fp);
  169         fchmod(fileno(fp), (S_IRUGO|S_IWUGO));
  170         fclose(fp);
  171     }
  172 }
  173 
  174 
  175 void
  176 debug_print_active(
  177     void)
  178 {
  179     FILE *fp;
  180     char file[PATH_LEN];
  181 
  182     if (!(debug & DEBUG_MISC))
  183         return;
  184 
  185     joinpath(file, sizeof(file), TMPDIR, "ACTIVE");
  186 
  187     if ((fp = fopen(file, "w")) != NULL) {
  188         int i;
  189         struct t_group *group;
  190 
  191         for_each_group(i) {
  192             group = &active[i];
  193             fprintf(fp, "[%4d]=[%s] type=[%s] spooldir=[%s]\n",
  194                 i, group->name,
  195                 (group->type == GROUP_TYPE_NEWS ? "NEWS" : "MAIL"),
  196                 group->spooldir);
  197             fprintf(fp, "count=[%4"T_ARTNUM_PFMT"] max=[%4"T_ARTNUM_PFMT"] min=[%4"T_ARTNUM_PFMT"] mod=[%c]\n",
  198                 group->count, group->xmax, group->xmin, group->moderated);
  199             fprintf(fp, " nxt=[%4d] hash=[%lu]  description=[%s]\n", group->next,
  200                 hash_groupname(group->name), BlankIfNull(group->description));
  201             if (debug & DEBUG_NEWSRC)
  202                 debug_print_newsrc(&group->newsrc, fp);
  203             if (debug & DEBUG_ATTRIB)
  204                 debug_print_attributes(group->attribute, fp);
  205         }
  206         fchmod(fileno(fp), (S_IRUGO|S_IWUGO));
  207         fclose(fp);
  208     }
  209 }
  210 
  211 
  212 static void
  213 debug_print_attributes(
  214     struct t_attribute *attr,
  215     FILE *fp)
  216 {
  217     if (attr == NULL)
  218         return;
  219 
  220     fprintf(fp, "global=[%u] show=[%u] thread=[%u] sort=[%u] author=[%u] auto_select=[%u] auto_save=[%u] batch_save=[%u] process=[%u]\n",
  221         attr->global,
  222         attr->show_only_unread_arts,
  223         attr->thread_articles,
  224         attr->sort_article_type,
  225         attr->show_author,
  226         attr->auto_select,
  227         attr->auto_save,
  228         attr->batch_save,
  229         attr->post_process_type);
  230     fprintf(fp, "select_header=[%u] select_global=[%s] select_expire=[%s]\n",
  231         attr->quick_select_header,
  232         BlankIfNull(attr->quick_select_scope),
  233         bool_unparse(attr->quick_select_expire));
  234     fprintf(fp, "kill_header  =[%u] kill_global  =[%s] kill_expire  =[%s]\n",
  235         attr->quick_kill_header,
  236         BlankIfNull(attr->quick_kill_scope),
  237         bool_unparse(attr->quick_kill_expire));
  238     fprintf(fp, "maildir=[%s] savedir=[%s] savefile=[%s]\n",
  239         BlankIfNull(attr->maildir),
  240         BlankIfNull(attr->savedir),
  241         BlankIfNull(attr->savefile));
  242     fprintf(fp, "sigfile=[%s] followup_to=[%s]\n\n",
  243         BlankIfNull(attr->sigfile),
  244         BlankIfNull(attr->followup_to));
  245     fflush(fp);
  246 }
  247 
  248 
  249 void
  250 debug_print_malloc(
  251     int is_malloc,
  252     const char *xfile,
  253     int line,
  254     size_t size)
  255 {
  256     FILE *fp;
  257     char file[PATH_LEN];
  258     static size_t total = 0;
  259 
  260     if (debug & DEBUG_MEM) {
  261         joinpath(file, sizeof(file), TMPDIR, "MALLOC");
  262         if ((fp = fopen(file, "a")) != NULL) {
  263             total += size;
  264             /* sometimes size_t is long */
  265             fprintf(fp, "%12s:%-4d %s(%6lu). Total %lu\n", xfile, line, is_malloc ? " malloc" : "realloc", (unsigned long) size, (unsigned long) total);
  266             fchmod(fileno(fp), (S_IRUGO|S_IWUGO));
  267             fclose(fp);
  268         }
  269     }
  270 }
  271 
  272 
  273 static void
  274 debug_print_filter(
  275     FILE *fp,
  276     int num,
  277     struct t_filter *the_filter)
  278 {
  279     static const char sign[] = { ' ', '=', '<', '>', '\0' };
  280 
  281     fprintf(fp, "[%3d]  group=[%s]\n       inscope=[%s] score=[%d] case=[%s]\n",
  282         num, BlankIfNull(the_filter->scope),
  283         (the_filter->inscope ? "TRUE" : "FILTER"),
  284         the_filter->score,
  285         the_filter->icase ? "C" : "I");
  286 
  287     if (the_filter->subj)
  288         fprintf(fp, "       subj=[%s]\n", the_filter->subj);
  289     if (the_filter->from)
  290         fprintf(fp, "       from=[%s]\n", the_filter->from);
  291     if (the_filter->msgid)
  292         fprintf(fp, "       msgid=[%s]\n", the_filter->msgid);
  293     if (the_filter->xref)
  294         fprintf(fp, "       xref=[%s]\n", the_filter->xref);
  295     if (the_filter->path)
  296         fprintf(fp, "       path=[%s]\n", the_filter->path);
  297 
  298     fprintf(fp, "       lines=[%c%d] gnksa=[%c%d]\n",
  299         sign[(int) the_filter->lines_cmp], the_filter->lines_num,
  300         sign[(int) the_filter->gnksa_cmp], the_filter->gnksa_num);
  301 
  302     if (the_filter->time)
  303         fprintf(fp, "       time=[%ld][%s]\n", (long) the_filter->time, BlankIfNull(str_trim(ctime(&the_filter->time))));
  304 }
  305 
  306 
  307 void
  308 debug_print_filters(
  309     void)
  310 {
  311     FILE *fp;
  312     char file[PATH_LEN];
  313     int i, num;
  314     struct t_filter *the_filter;
  315 
  316     if (!(debug & DEBUG_FILTER))
  317         return;
  318 
  319     joinpath(file, sizeof(file), TMPDIR, "FILTER");
  320 
  321     if ((fp = fopen(file, "w")) != NULL) {
  322         /*
  323          * print global filter
  324          */
  325         num = glob_filter.num;
  326         the_filter = glob_filter.filter;
  327         fprintf(fp, "*** BEG GLOBAL FILTER=[%3d] ***\n", num);
  328         for (i = 0; i < num; i++) {
  329             debug_print_filter(fp, i, &the_filter[i]);
  330             fprintf(fp, "\n");
  331         }
  332         fprintf(fp, "*** END GLOBAL FILTER ***\n");
  333 
  334         fchmod(fileno(fp), (S_IRUGO|S_IWUGO));
  335         fclose(fp);
  336     }
  337 }
  338 
  339 
  340 void
  341 debug_print_file(
  342     const char *fname,
  343     const char *fmt,
  344     ...)
  345 {
  346     FILE *fp;
  347     char *buf;
  348     char file[PATH_LEN];
  349     va_list ap;
  350 
  351     if (!debug)
  352         return;
  353 
  354     va_start(ap, fmt);
  355     buf = fmt_message(fmt, ap);
  356     va_end(ap);
  357 
  358     joinpath(file, sizeof(file), TMPDIR, fname);
  359 
  360     if ((fp = fopen(file, "a")) != NULL) {
  361         fprintf(fp,"%s\n", buf);
  362         fchmod(fileno(fp), (S_IRUGO|S_IWUGO));
  363         fclose(fp);
  364     }
  365     free(buf);
  366 }
  367 
  368 
  369 void
  370 debug_print_comment(
  371     const char *comment)
  372 {
  373     if (!(debug & DEBUG_NEWSRC))
  374         return;
  375 
  376     debug_print_file("BITMAP", comment);
  377 }
  378 
  379 
  380 void
  381 debug_print_bitmap(
  382     struct t_group *group,
  383     struct t_article *art)
  384 {
  385     FILE *fp;
  386     char file[PATH_LEN];
  387 
  388     if (!(debug & DEBUG_NEWSRC))
  389         return;
  390 
  391     joinpath(file, sizeof(file), TMPDIR, "BITMAP");
  392     if (group != NULL) {
  393         if ((fp = fopen(file, "a")) != NULL) {
  394             fprintf(fp, "\nActive: Group=[%s] sub=[%c] min=[%"T_ARTNUM_PFMT"] max=[%"T_ARTNUM_PFMT"] count=[%"T_ARTNUM_PFMT"] num_unread=[%"T_ARTNUM_PFMT"]\n",
  395                 group->name, SUB_CHAR(group->subscribed),
  396                 group->xmin, group->xmax, group->count,
  397                 group->newsrc.num_unread);
  398             if (art != NULL) {
  399                 fprintf(fp, "art=[%5"T_ARTNUM_PFMT"] tag=[%s] kill=[%s] selected=[%s] subj=[%s]\n",
  400                     art->artnum,
  401                     bool_unparse(art->tagged),
  402                     bool_unparse(art->killed),
  403                     bool_unparse(art->selected),
  404                     art->subject);
  405                 fprintf(fp, "thread=[%d]  prev=[%d]  status=[%s]\n",
  406                     art->thread, art->prev,
  407                     (art->status == ART_READ ? "READ" : "UNREAD"));
  408             }
  409             debug_print_newsrc(&group->newsrc, fp);
  410             fchmod(fileno(fp), (S_IRUGO|S_IWUGO));
  411             fclose(fp);
  412         }
  413     }
  414 }
  415 
  416 
  417 static void
  418 debug_print_newsrc(
  419     struct t_newsrc *lnewsrc,
  420     FILE *fp)
  421 {
  422     int j;
  423     t_artnum i;
  424 
  425     fprintf(fp, "Newsrc: min=[%"T_ARTNUM_PFMT"] max=[%"T_ARTNUM_PFMT"] bitlen=[%"T_ARTNUM_PFMT"] num_unread=[%"T_ARTNUM_PFMT"] present=[%d]\n",
  426         lnewsrc->xmin, lnewsrc->xmax, lnewsrc->xbitlen,
  427         lnewsrc->num_unread, (lnewsrc->present ? 1 : 0));
  428 
  429     fprintf(fp, "bitmap=[");
  430     if (lnewsrc->xbitlen && lnewsrc->xbitmap) {
  431         for (j = 0, i = lnewsrc->xmin; i <= lnewsrc->xmax; i++) {
  432             fprintf(fp, "%d",
  433                 (NTEST(lnewsrc->xbitmap, i - lnewsrc->xmin) == ART_READ ?
  434                 ART_READ : ART_UNREAD));
  435             if ((j++ % 8) == 7 && i < lnewsrc->xmax)
  436                 fprintf(fp, " ");
  437         }
  438     }
  439     fprintf(fp, "]\n");
  440     fflush(fp);
  441 }
  442 
  443 
  444 #   ifdef NNTP_ABLE
  445 const char *
  446 logtime(
  447     void)
  448 {
  449 #       if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETTIMEOFDAY)
  450     static struct t_tintime log_time;
  451     static char out[40];
  452 
  453     if (tin_gettime(&log_time) == 0) {
  454         if (my_strftime(out, 39, " [%H:%M:%S.", gmtime(&(log_time.tv_sec)))) {
  455             sprintf(out + 11, "%09ld", log_time.tv_nsec); /* strlen(" [hh:mm:ss.") */
  456             out[17] = '\0'; /* strlen(" [hh:mm:ss.uuuuuu") */
  457             strcat(out, "] ");
  458             return out;
  459         }
  460     }
  461 #       endif /* HAVE_CLOCK_GETTIME || HAVE_GETTIMEOFDAY */
  462     return " ";
  463 }
  464 #   endif /* NNTP_ABLE */
  465 #endif /* DEBUG */