syslog_client_misc.c
Go to the documentation of this file.
1 /**
2  * @file syslog_client_misc.c
3  * @brief Helper functions for Syslog client
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SYSLOG_TRACE_LEVEL
33 
34 //Dependencies
35 #include "syslog/syslog_client.h"
37 #include "debug.h"
38 
39 //Check TCP/IP stack configuration
40 #if (SYSLOG_CLIENT_SUPPORT == ENABLED)
41 
42 
43 /**
44  * @brief Format timestamp
45  * @param[in] time Unix timestamp
46  * @param[out] buffer Buffer where to store the formatted string
47  * @return Length of the formatted string
48  **/
49 
50 size_t syslogClientFormatTimestamp(time_t time, char_t *buffer)
51 {
52  size_t n;
53  DateTime dateTime;
54 
55  //Abbreviated months
56  static const char_t months[13][4] =
57  {
58  " ",
59  "Jan",
60  "Feb",
61  "Mar",
62  "Apr",
63  "May",
64  "Jun",
65  "Jul",
66  "Aug",
67  "Sep",
68  "Oct",
69  "Nov",
70  "Dec"
71  };
72 
73  //Convert Unix timestamp to date
74  convertUnixTimeToDate(time, &dateTime);
75 
76  //The format of the timestamp field is Mmm dd hh:mm:ss. If the day of the
77  //month is less than 10, then it must be represented as a space and then the
78  //number (refer to RFC 3164, section 4.1.2)
79  n = osSprintf(buffer, "%s %2" PRIu8 " %02" PRIu8 ":%02" PRIu8 ":%02" PRIu8,
80  months[dateTime.month], dateTime.day, dateTime.hours, dateTime.minutes,
81  dateTime.seconds);
82 
83  //Return the length of the formatted string
84  return n;
85 }
86 
87 #endif
char char_t
Definition: compiler_port.h:48
void convertUnixTimeToDate(time_t t, DateTime *date)
Convert Unix timestamp to date.
Definition: date_time.c:198
Debugging facilities.
uint8_t n
uint32_t time
#define osSprintf(dest,...)
Definition: os_port.h:231
Date and time representation.
Definition: date_time.h:47
uint8_t hours
Definition: date_time.h:52
uint8_t month
Definition: date_time.h:49
uint8_t seconds
Definition: date_time.h:54
uint8_t day
Definition: date_time.h:50
uint8_t minutes
Definition: date_time.h:53
Syslog client.
size_t syslogClientFormatTimestamp(time_t time, char_t *buffer)
Format timestamp.
Helper functions for Syslog client.