Subversion Repositories Kolibri OS

Rev

Rev 3584 | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org>
  3.  * Copyright 2004-2007 James Bursa <bursa@users.sourceforge.net>
  4.  * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
  5.  * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
  6.  * Copyright 2004 John Tytgat <joty@netsurf-browser.org>
  7.  *
  8.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  9.  *
  10.  * NetSurf is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; version 2 of the License.
  13.  *
  14.  * NetSurf is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.  */
  22.  
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <sys/time.h>
  26. #include "desktop/netsurf.h"
  27.  
  28. #include "utils/log.h"
  29.  
  30. nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv)
  31. {
  32.         nserror ret = NSERROR_OK;
  33.  
  34.         if (((*pargc) > 1) &&
  35.             (argv[1][0] == '-') &&
  36.             (argv[1][1] == 'v') &&
  37.             (argv[1][2] == 0)) {
  38.                 int argcmv;
  39.                 for (argcmv = 2; argcmv < (*pargc); argcmv++) {
  40.                         argv[argcmv - 1] = argv[argcmv];
  41.                 }
  42.                 (*pargc)--;
  43.  
  44.                 /* ensure we actually show logging */
  45.                 verbose_log = true;
  46.                
  47.                 /* ensure stderr is available */
  48.                 if (ensure != NULL) {
  49.                         if (ensure(stderr) == false) {
  50.                                 /* failed to ensure output */
  51.                                 ret = NSERROR_INIT_FAILED;
  52.                         }
  53.                 }
  54.         }
  55.         return ret;
  56. }
  57.  
  58. #ifndef NDEBUG
  59.  
  60. /* Subtract the `struct timeval' values X and Y,
  61.    storing the result in RESULT.
  62.    Return 1 if the difference is negative, otherwise 0.  
  63. */
  64.  
  65. static int
  66. timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
  67. {
  68.         /* Perform the carry for the later subtraction by updating y. */
  69.         if (x->tv_usec < y->tv_usec) {
  70.                 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  71.                 y->tv_usec -= 1000000 * nsec;
  72.                 y->tv_sec += nsec;
  73.         }
  74.         if (x->tv_usec - y->tv_usec > 1000000) {
  75.                 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  76.                 y->tv_usec += 1000000 * nsec;
  77.                 y->tv_sec -= nsec;
  78.         }
  79.  
  80.         /* Compute the time remaining to wait.
  81.            tv_usec is certainly positive. */
  82.         result->tv_sec = x->tv_sec - y->tv_sec;
  83.         result->tv_usec = x->tv_usec - y->tv_usec;
  84.  
  85.         /* Return 1 if result is negative. */
  86.         return x->tv_sec < y->tv_sec;
  87. }
  88.  
  89. const char *nslog_gettime(void)
  90. {
  91.         static struct timeval start_tv;
  92.         static char buff[32];
  93.  
  94.         struct timeval tv;
  95.         struct timeval now_tv;
  96.  
  97.         if (!timerisset(&start_tv)) {
  98.                 gettimeofday(&start_tv, NULL);         
  99.         }
  100.         gettimeofday(&now_tv, NULL);
  101.  
  102.         timeval_subtract(&tv, &now_tv, &start_tv);
  103.  
  104.         snprintf(buff, sizeof(buff),"(%ld.%06ld)",
  105.                         (long)tv.tv_sec, (long)tv.tv_usec);
  106.  
  107.         return buff;
  108. }
  109.  
  110. void nslog_log(const char *format, ...)
  111. {
  112.         va_list ap;
  113.  
  114.         va_start(ap, format);
  115.  
  116.         vfprintf(stderr, format, ap);
  117.  
  118.         va_end(ap);
  119. }
  120.  
  121. #endif
  122.  
  123.