LCOV - code coverage report
Current view: top level - snaplogger - date_variable.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 141 23.4 %
Date: 2021-05-29 11:58:38 Functions: 19 31 61.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013-2021  Made to Order Software Corp.  All Rights Reserved
       3             :  *
       4             :  * https://snapwebsites.org/project/snaplogger
       5             :  * contact@m2osw.com
       6             :  *
       7             :  * This program is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation; either version 2 of the License, or
      10             :  * (at your option) any later version.
      11             :  *
      12             :  * This program is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License along
      18             :  * with this program; if not, write to the Free Software Foundation, Inc.,
      19             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      20             :  */
      21             : 
      22             : /** \file
      23             :  * \brief Implementation of the date, time, locale variables.
      24             :  *
      25             :  * This file implements a set of variables offering ways to display the
      26             :  * message timestamp date and time.
      27             :  */
      28             : 
      29             : // self
      30             : //
      31             : #include    "snaplogger/exception.h"
      32             : #include    "snaplogger/variable.h"
      33             : 
      34             : 
      35             : // last include
      36             : //
      37             : #include    <snapdev/poison.h>
      38             : 
      39             : 
      40             : 
      41             : namespace snaplogger
      42             : {
      43             : 
      44             : 
      45             : namespace
      46             : {
      47             : 
      48             : 
      49             : constexpr char const * g_day_name[] =
      50             : {
      51             :     "Sunday",
      52             :     "Monday",
      53             :     "Tuesday",
      54             :     "Wednesday",
      55             :     "Thursday",
      56             :     "Friday",
      57             :     "Saturday"
      58             : };
      59             : 
      60             : constexpr char const * g_month_name[] =
      61             : {
      62             :     "January",
      63             :     "February",
      64             :     "Marsh",
      65             :     "April",
      66             :     "May",
      67             :     "June",
      68             :     "July",
      69             :     "August",
      70             :     "September",
      71             :     "October",
      72             :     "November",
      73             :     "December"
      74             : };
      75             : 
      76           4 : timespec const g_start_date = []() { timespec tp; clock_gettime(CLOCK_REALTIME_COARSE, &tp); return tp; }();
      77             : 
      78             : 
      79             : 
      80             : 
      81             : 
      82          23 : DEFINE_LOGGER_VARIABLE(date)
      83             : {
      84             :     tm t;
      85           3 :     timespec const & timestamp(msg.get_timestamp());
      86           3 :     gmtime_r(&timestamp.tv_sec, &t);
      87             : 
      88           6 :     std::string date_format("%Y/%m/%d");
      89           6 :     auto params(get_params());
      90           3 :     if(!params.empty())
      91             :     {
      92           6 :         auto const p(params[0]->get_name());
      93           3 :         if(p == "day")
      94             :         {
      95           1 :             date_format = "%d";
      96             :         }
      97           2 :         else if(p == "day_of_week_name")
      98             :         {
      99           0 :             value += g_day_name[t.tm_wday];
     100           0 :             date_format.clear();
     101             :         }
     102           2 :         else if(p == "day_of_week")
     103             :         {
     104           0 :             date_format = "%w"; // TBD: people may want %u though...
     105             :         }
     106           2 :         else if(p == "year_week")
     107             :         {
     108           0 :             date_format = "%U"; // TBD: people may want %V or %W though...
     109             :         }
     110           2 :         else if(p == "year_day")
     111             :         {
     112           0 :             date_format = "%j";
     113             :         }
     114           2 :         else if(p == "month_name")
     115             :         {
     116           0 :             value += g_month_name[t.tm_mon];
     117           0 :             date_format.clear();
     118             :         }
     119           2 :         else if(p == "month")
     120             :         {
     121           1 :             date_format = "%m";
     122             :         }
     123           1 :         else if(p == "year")
     124             :         {
     125           1 :             date_format = "%Y";
     126             :         }
     127             :         else
     128             :         {
     129           0 :             throw invalid_variable("the ${hostbyname:...} variable first parameter must be its name parameter.");
     130             :         }
     131             :     }
     132             : 
     133           3 :     if(!date_format.empty())
     134             :     {
     135             :         char buf[256];
     136           3 :         strftime(buf, sizeof(buf), date_format.c_str(), &t);
     137           3 :         buf[sizeof(buf) - 1] = '\0';
     138           3 :         if(buf[0] == '0')
     139             :         {
     140           1 :             int n(0);
     141           2 :             for(; buf[n] == '0'; ++n);
     142           1 :             if(buf[n] == '\0')
     143             :             {
     144             :                 // keep at least one zero
     145             :                 //
     146           0 :                 value += '0';
     147             :             }
     148             :             else
     149             :             {
     150           1 :                 value += buf + n;
     151             :             }
     152             :         }
     153             :         else
     154             :         {
     155           2 :             value += buf;
     156             :         }
     157             :     }
     158             : 
     159           3 :     variable::process_value(msg, value);
     160           3 : }
     161             : 
     162             : 
     163          11 : DEFINE_LOGGER_VARIABLE(time)
     164             : {
     165           0 :     auto nanosec = [](timespec const & tp)
     166             :     {
     167           0 :         std::string sec(std::to_string(tp.tv_sec));
     168           0 :         std::string nsec(std::to_string(tp.tv_nsec));
     169           0 :         if(nsec.length() < 9)
     170             :         {
     171           0 :             nsec = std::string("000000000").substr(0, 9 - nsec.length()) + nsec;
     172             :         }
     173           0 :         return sec + nsec;
     174             :     };
     175             : 
     176             :     tm t;
     177           0 :     timespec timestamp(msg.get_timestamp());
     178           0 :     gmtime_r(&timestamp.tv_sec, &t);
     179             : 
     180           0 :     std::string time_format("%H:%M:%S");
     181           0 :     auto params(get_params());
     182           0 :     if(!params.empty())
     183             :     {
     184           0 :         auto const p(params[0]->get_name());
     185           0 :         if(p == "hour")
     186             :         {
     187           0 :             if(params[0]->get_value() == "12")
     188             :             {
     189           0 :                 time_format = "%I";
     190             :             }
     191             :             else
     192             :             {
     193           0 :                 time_format = "%H";
     194             :             }
     195             :         }
     196           0 :         else if(p == "minute")
     197             :         {
     198           0 :             time_format = "%M"; // TBD: people may want %u though...
     199             :         }
     200           0 :         else if(p == "second")
     201             :         {
     202           0 :             time_format = "%S"; // TBD: people may want %V or %W though...
     203             :         }
     204           0 :         else if(p == "millisecond")
     205             :         {
     206           0 :             value += std::to_string(timestamp.tv_nsec / 1000000LL);
     207           0 :             time_format.clear();
     208             :         }
     209           0 :         else if(p == "microsecond")
     210             :         {
     211           0 :             value += std::to_string(timestamp.tv_nsec / 1000LL);
     212           0 :             time_format.clear();
     213             :         }
     214           0 :         else if(p == "nanosecond")
     215             :         {
     216           0 :             value += std::to_string(timestamp.tv_nsec);
     217           0 :             time_format.clear();
     218             :         }
     219           0 :         else if(p == "unix")
     220             :         {
     221           0 :             value += std::to_string(timestamp.tv_sec);
     222           0 :             time_format.clear();
     223             :         }
     224           0 :         else if(p == "meridiem")
     225             :         {
     226             :             // TODO: we should force English here (AM or PM)
     227             :             //
     228           0 :             time_format = "%p";
     229             :         }
     230           0 :         else if(p == "offset")
     231             :         {
     232             :             timespec offset;
     233           0 :             offset.tv_nsec = timestamp.tv_nsec - g_start_date.tv_nsec;
     234           0 :             offset.tv_sec = timestamp.tv_sec - g_start_date.tv_sec;
     235           0 :             if(offset.tv_nsec < 0)
     236             :             {
     237           0 :                 --offset.tv_sec;
     238           0 :                 offset.tv_nsec += 1000000000;
     239             :             }
     240           0 :             value += nanosec(offset);
     241           0 :             time_format.clear();
     242             :         }
     243           0 :         else if(p == "process")
     244             :         {
     245           0 :             clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &timestamp);
     246           0 :             value += nanosec(timestamp);
     247           0 :             time_format.clear();
     248             :         }
     249           0 :         else if(p == "thread")
     250             :         {
     251           0 :             clock_gettime(CLOCK_THREAD_CPUTIME_ID, &timestamp);
     252           0 :             value += nanosec(timestamp);
     253           0 :             time_format.clear();
     254             :         }
     255           0 :         else if(p == "process_ms")
     256             :         {
     257           0 :             clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &timestamp);
     258           0 :             gmtime_r(&timestamp.tv_sec, &t);
     259           0 :             std::string ms(std::to_string(timestamp.tv_nsec / 1000000UL));
     260           0 :             if(ms.length() < 3)
     261             :             {
     262           0 :                 ms = std::string("000").substr(0, 3 - ms.length());
     263             :             }
     264           0 :             time_format += ".";
     265           0 :             time_format += ms;
     266             :         }
     267           0 :         else if(p == "thread_ms")
     268             :         {
     269           0 :             clock_gettime(CLOCK_THREAD_CPUTIME_ID, &timestamp);
     270           0 :             gmtime_r(&timestamp.tv_sec, &t);
     271           0 :             std::string ms(std::to_string(timestamp.tv_nsec / 1000000UL));
     272           0 :             if(ms.length() < 3)
     273             :             {
     274           0 :                 ms = std::string("000").substr(0, 3 - ms.length());
     275             :             }
     276           0 :             time_format += ".";
     277           0 :             time_format += ms;
     278             :         }
     279             :         else
     280             :         {
     281           0 :             throw invalid_variable("the ${hostbyname:...} variable first parameter must be its name parameter.");
     282             :         }
     283             :     }
     284             : 
     285           0 :     if(!time_format.empty())
     286             :     {
     287             :         char buf[256];
     288           0 :         strftime(buf, sizeof(buf), time_format.c_str(), &t);
     289           0 :         buf[sizeof(buf) - 1] = '\0';
     290           0 :         value += buf;
     291             :     }
     292             : 
     293             : 
     294           0 :     variable::process_value(msg, value);
     295           0 : }
     296             : 
     297             : 
     298           8 : DEFINE_LOGGER_VARIABLE(locale)
     299             : {
     300             :     tm t;
     301           0 :     timespec const & timestamp(msg.get_timestamp());
     302           0 :     localtime_r(&timestamp.tv_sec, &t);
     303             : 
     304           0 :     std::string locale_format("%c");
     305           0 :     auto params(get_params());
     306           0 :     if(!params.empty())
     307             :     {
     308           0 :         auto const p(params[0]->get_name());
     309           0 :         if(p == "day_of_week_name")
     310             :         {
     311           0 :             locale_format = "%A";   // TBD: support %a as well
     312             :         }
     313           0 :         else if(p == "month_name")
     314             :         {
     315           0 :             locale_format = "%B";     // TBD: support %b as well
     316             :         }
     317           0 :         else if(p == "date")
     318             :         {
     319           0 :             locale_format = "%x";
     320             :         }
     321           0 :         else if(p == "time")
     322             :         {
     323           0 :             locale_format = "%X";
     324             :         }
     325           0 :         else if(p == "meridiem")
     326             :         {
     327           0 :             locale_format = "%p";
     328             :         }
     329           0 :         else if(p == "timezone")
     330             :         {
     331           0 :             locale_format = "%Z";
     332             :         }
     333           0 :         else if(p == "timezone_offset")
     334             :         {
     335           0 :             locale_format = "%z";
     336             :         }
     337             :         // else -- assume params are system parameters
     338             :     }
     339             : 
     340           0 :     if(!locale_format.empty())
     341             :     {
     342             :         char buf[256];
     343           0 :         strftime(buf, sizeof(buf), locale_format.c_str(), &t);
     344           0 :         buf[sizeof(buf) - 1] = '\0';
     345           0 :         value += buf;
     346             :     }
     347             : 
     348           0 :     variable::process_value(msg, value);
     349           0 : }
     350             : 
     351             : 
     352             : }
     353             : // no name namespace
     354             : 
     355             : 
     356           6 : } // snaplogger namespace
     357             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13