htsky

Load and structure a vertically stratified atmosphere
git clone git://git.meso-star.fr/htsky.git
Log | Files | Refs | README | LICENSE

htsky_log.c (4666B)


      1 /* Copyright (C) 2018, 2019, 2020, 2021 |Méso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2018, 2019 Centre National de la Recherche Scientifique
      3  * Copyright (C) 2018, 2019 Université Paul Sabatier
      4  *
      5  * This program is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     17 
     18 #define _POSIX_C_SOURCE 200112L /* snprintf support */
     19 
     20 #include "htsky_c.h"
     21 #include "htsky_log.h"
     22 
     23 #include <rsys/logger.h>
     24 
     25 #include <stdarg.h>
     26 
     27 /*******************************************************************************
     28  * Helper functions
     29  ******************************************************************************/
     30 static INLINE void
     31 log_msg
     32   (const struct htsky* sky,
     33    const enum log_type stream,
     34    const char* msg,
     35    va_list vargs)
     36 {
     37   ASSERT(sky && msg);
     38   if(sky->verbose) {
     39     res_T res; (void)res;
     40     res = logger_vprint(sky->logger, stream, msg, vargs);
     41     ASSERT(res == RES_OK);
     42   }
     43 }
     44 
     45 static void
     46 print_info(const char* msg, void* ctx)
     47 {
     48   (void)ctx;
     49   fprintf(stderr, MSG_INFO_PREFIX"%s", msg);
     50 }
     51 
     52 static void
     53 print_err(const char* msg, void* ctx)
     54 {
     55   (void)ctx;
     56   fprintf(stderr, MSG_ERROR_PREFIX"%s", msg);
     57 }
     58 
     59 static void
     60 print_warn(const char* msg, void* ctx)
     61 {
     62   (void)ctx;
     63   fprintf(stderr, MSG_WARNING_PREFIX"%s", msg);
     64 }
     65 
     66 static res_T
     67 setup_default_logger(struct mem_allocator* allocator, struct logger* logger)
     68 {
     69   res_T res = RES_OK;
     70   ASSERT(logger);
     71   res = logger_init(allocator, logger);
     72   if(res != RES_OK) return res;
     73   logger_set_stream(logger, LOG_OUTPUT, print_info, NULL);
     74   logger_set_stream(logger, LOG_ERROR, print_err, NULL);
     75   logger_set_stream(logger, LOG_WARNING, print_warn, NULL);
     76   return RES_OK;
     77 }
     78 
     79 /*******************************************************************************
     80  * Local functions
     81  ******************************************************************************/
     82 res_T
     83 setup_log_default(struct htsky* sky)
     84 {
     85   res_T res = RES_OK;
     86   ASSERT(sky);
     87 
     88   res = setup_default_logger(sky->allocator, &sky->logger__);
     89   if(res != RES_OK) {
     90     if(sky->verbose) {
     91       fprintf(stderr, MSG_ERROR_PREFIX "could not setup the HTSky logger.\n");
     92     }
     93     goto error;
     94   }
     95   sky->logger = &sky->logger__;
     96 
     97 exit:
     98   return res;
     99 error:
    100   goto exit;
    101 }
    102 
    103 void
    104 log_info(const struct htsky* sky, const char* msg, ...)
    105 {
    106   va_list vargs_list;
    107   ASSERT(sky && msg);
    108 
    109   va_start(vargs_list, msg);
    110   log_msg(sky, LOG_OUTPUT, msg, vargs_list);
    111   va_end(vargs_list);
    112 }
    113 
    114 void
    115 log_err(const struct htsky* sky, const char* msg, ...)
    116 {
    117   va_list vargs_list;
    118   ASSERT(sky && msg);
    119 
    120   va_start(vargs_list, msg);
    121   log_msg(sky, LOG_ERROR, msg, vargs_list);
    122   va_end(vargs_list);
    123 }
    124 
    125 void
    126 log_warn(const struct htsky* sky, const char* msg, ...)
    127 {
    128   va_list vargs_list;
    129   ASSERT(sky && msg);
    130 
    131   va_start(vargs_list, msg);
    132   log_msg(sky, LOG_WARNING, msg, vargs_list);
    133   va_end(vargs_list);
    134 }
    135 
    136 void
    137 log_svx_memory_usage(struct htsky* sky)
    138 {
    139   char dump[128];
    140   char* dst = dump;
    141   size_t available_space = sizeof(dump);
    142   const size_t KILO_BYTE = 1024;
    143   const size_t MEGA_BYTE = 1024*KILO_BYTE;
    144   const size_t GIGA_BYTE = 1024*MEGA_BYTE;
    145   size_t ngigas, nmegas, nkilos, memsz, len;
    146   ASSERT(sky);
    147 
    148   memsz = MEM_ALLOCATED_SIZE(&sky->svx_allocator);
    149 
    150   if((ngigas = memsz / GIGA_BYTE) != 0) {
    151     len = (size_t)snprintf(dst, available_space,
    152       "%lu GB ", (unsigned long)ngigas);
    153     CHK(len < available_space);
    154     dst += len;
    155     available_space -= len;
    156     memsz -= ngigas * GIGA_BYTE;
    157   }
    158   if((nmegas = memsz / MEGA_BYTE) != 0) {
    159     len = (size_t)snprintf(dst, available_space,
    160       "%lu MB ", (unsigned long)nmegas);
    161     CHK(len < available_space);
    162     dst += len;
    163     available_space -= len;
    164     memsz -= nmegas * MEGA_BYTE;
    165   }
    166   if((nkilos = memsz / KILO_BYTE) != 0) {
    167     len = (size_t)snprintf(dst, available_space,
    168       "%lu KB ", (unsigned long)nkilos);
    169     dst += len;
    170     available_space -= len;
    171     memsz -= nkilos * KILO_BYTE;
    172   }
    173   if(memsz) {
    174     len = (size_t)snprintf(dst, available_space,
    175       "%lu Byte%s", (unsigned long)memsz, memsz > 1 ? "s" : "");
    176     CHK(len < available_space);
    177   }
    178   log_info(sky, "SVX memory usage: %s\n", dump);
    179 }
    180