1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 #pragma once
24 
25 #include <ctime>
26 
27 #include "tscore/ink_platform.h"
28 #include "tscore/Arena.h"
29 #include <tscpp/util/TextView.h>
30 
31 class MIMEHdr;
32 
33 namespace LogUtils
34 {
35 enum AlarmType {
36   LOG_ALARM_ERROR = 0,
37   LOG_ALARM_WARNING,
38   LOG_ALARM_N_TYPES,
39 };
40 
41 static inline long
timestamp()42 timestamp()
43 {
44   return (long)time(nullptr);
45 }
46 
47 int timestamp_to_str(long timestamp, char *buf, int size);
48 char *timestamp_to_netscape_str(long timestamp);
49 char *timestamp_to_date_str(long timestamp);
50 char *timestamp_to_time_str(long timestamp);
51 unsigned ip_from_host(char *host);
52 void manager_alarm(AlarmType alarm_type, const char *msg, ...) TS_PRINTFLIKE(2, 3);
53 void strip_trailing_newline(char *buf);
54 char *escapify_url(Arena *arena, char *url, size_t len_in, int *len_out, char *dst = nullptr, size_t dst_size = 0,
55                    const unsigned char *map = nullptr);
56 char *pure_escapify_url(Arena *arena, char *url, size_t len_in, int *len_out, char *dst = nullptr, size_t dst_size = 0,
57                         const unsigned char *map = nullptr);
58 char *int64_to_str(char *buf, unsigned int buf_size, int64_t val, unsigned int *total_chars, unsigned int req_width = 0,
59                    char pad_char = '0');
60 void remove_content_type_attributes(char *type_str, int *type_len);
61 int timestamp_to_hex_str(unsigned timestamp, char *str, size_t len, size_t *n_chars = nullptr);
62 int seconds_to_next_roll(time_t time_now, int rolling_offset, int rolling_interval);
63 int file_is_writeable(const char *full_filename, off_t *size_bytes = nullptr, bool *has_size_limit = nullptr,
64                       uint64_t *current_size_limit_bytes = nullptr);
65 
66 /** Given a rolled file, determine the unrolled filename.
67  *
68  * For example, given this:
69  *   diags.log.20191114.21h43m16s-20191114.21h43m17s.old
70  *
71  * Return this:
72  *   diags.log
73  *
74  * @param[in] rolled_filename The rolled filename from which to derive the
75  * unrolled filename.
76  *
77  * @return The unrolled filename if it looked like a rolled log file or the
78  * input filename if it didn't.
79  */
80 ts::TextView get_unrolled_filename(ts::TextView rolled_filename);
81 
82 // Marshals header tags and values together, with a single terminating nul character.  Returns buffer space required.  'buf' points
83 // to where to put the marshaled data.  If 'buf' is null, no data is marshaled, but the function returns the amount of space that
84 // would have been used.
85 int marshalMimeHdr(MIMEHdr *hdr, char *buf);
86 
87 // Unmarshelled/printable format is {{{tag1}:{value1}}{{tag2}:{value2}} ... }
88 //
89 // Returns -1 if data corruption is detected, otherwise the actual amount of data put into the 'dest' buffer.  '*buf' is advanced
90 // to byte after the last byte consumed.
91 int unmarshalMimeHdr(char **buf, char *dest, int destLength);
92 
93 }; // namespace LogUtils
94