aboutsummaryrefslogtreecommitdiff
path: root/src/aklog.c
blob: 248b64719a1df9c7c2929266c0450da511a0811c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#define AK_DEBUG true

void ak_log_print_log_line(char* line)
{
    if ( line )
    {
        int i = 0;
        int spaces_found = 0;
        int last_space = -1;
        long int l = 1000000000;
        long int ts = 0;
        struct tm *timeInfo;
        char ts_string[16]; // %Y%Y%Y%Y%m%m%d%d_%H%H%M%M%S%S
        while ( line[i] != '\0' )
        {
            if ( line[i] == ' ' ) //  && spaces_found < 4)
            {
                spaces_found++;
                if (true) //( spaces_found < 4 )
                {
                    for ( int k = last_space+1; k < i; k++ )
                    {
                        switch(spaces_found){
                            case 1:
                                // TS
                                while (true)
                                {
                                    if ( line[k] == ' ' )
                                    {
                                        timeInfo = localtime(&ts);
                                        strftime(ts_string, sizeof(ts_string), "%Y%m%d_%H%M%S", timeInfo);
                                        printf("%s ", ts_string);
                                        break;
                                    }
                                    else
                                    {
                                        switch(line[k])
                                        {
                                            case '0':
                                                ts = 0*l + ts;
                                                break;
                                            case '1':
                                                ts = 1*l + ts;
                                                break;
                                            case '2':
                                                ts = 2*l + ts;
                                                break;
                                            case '3':
                                                ts = 3*l + ts;
                                                break;
                                            case '4':
                                                ts = 4*l + ts;
                                                break;
                                            case '5':
                                                ts = 5*l + ts;
                                                break;
                                            case '6':
                                                ts = 6*l + ts;
                                                break;
                                            case '7':
                                                ts = 7*l + ts;
                                                break;
                                            case '8':
                                                ts = 8*l + ts;
                                                break;
                                            case '9':
                                                ts = 9*l + ts;
                                                break;
                                        }
                                        l = l/10;
                                    }
                                    k++;
                                }
                                break;
                            case 2:
                                // PROGRAM
                                printf("\033[1;32m");
                                while (true)
                                {
                                    if ( line[k] == ' ' )
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        printf("%c", line[k]);
                                    }
                                    k++;
                                }
                                break;
                            case 3:
                                // TYPE
                                printf("\033[0;00m \033[1;31m");
                                while (true)
                                {
                                    if ( line[k] == ' ' )
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        printf("%c", line[k]);
                                    }
                                    k++;
                                }
                                break;
                            case 4:
                                // MESSAGE
                                printf("\033[0;00m ");
                                while (true)
                                {
                                    if ( line[k] == '\0' )
                                    {
                                        printf("\n");
                                        break;
                                    }
                                    else
                                    {
                                        printf("%c", line[k]);
                                    }
                                    k++;
                                }
                                break;
                        }
                    }
                    last_space = i;
                }
            }
            i++;
        }
    }
}

void ak_log_follow()
{
    //    tail -f $AK_LOGSFILE | while read -r p || [ -n "$p" ]
    //    do
    //        ak_log_print_log_line "$p"
    //    done
}

void ak_log_grep(char* message)
{
    printf("ak_log_grep: not implemented\n");
    return;
    exit(2);
    if ( message )
    {
        if ( strcmp(message, "-h") || strcmp(message, "--help") )
        {
            // description();
            printf("Launch with no arguments and select from the menu that will appear\n");
            exit(1);
        }
    }

    printf("The following scripts have entries in the log file.\n");
    printf("Select one of those by entering the number of it below and hit enter:\n");
    //    select x in $(cat $AK_LOGSFILE | cut -d ' ' -f 2 | sort | uniq)
    //    do
    //        grep $x $AK_LOGSFILE | while read line
    //        do
    //            ak_log_print_log_line "$line"
    //        done
    //        break
    //    done
}

void ak_log_rotate()
{
    //    if [ -f $AK_LOGSFILE ]
    //    then
    //        tar cvfz $AK_ARCHIVESDIR/logs_$(date -u +%s).tar.gz $AK_WORKDIR/logs
    //        cat /dev/null > $AK_WORKDIR/logs
    //    fi
    //    if [ -f $AK_WORKDIR/akd.log ]
    //    then
    //        tar cvfz $AK_ARCHIVESDIR/akd-logs_$(date -u +%s).tar.gz $AK_WORKDIR/akd.log
    //        cat /dev/null > $AK_WORKDIR/akd.log
    //    fi
    printf("ak_log_rotate: not implemented\n");
    return;
    exit(2);
}

void ak_log_message(char* program, char* type, char* message)
{
    time_t ts = time(NULL);
    struct tm *timeInfo;
    char ts_string[16]; // %Y%Y%Y%Y%m%m%d%d_%H%H%M%M%S%S
    time(&ts);
    timeInfo = localtime(&ts);
    char* some_string = {0};
    strftime(ts_string, sizeof(ts_string), "%Y%m%d_%H%M%S", timeInfo);
    if ( program != NULL )
    {
        if ( type != NULL )
        {
            if ( message != NULL )
            {
                // msg="$(echo -n $*| cut -d ' ' -f 3-)"
                // echo "$TS" "<$1>" "[$2]" "$msg" >> $AK_LOGSFILE
                printf( "%ld <%s> [%s] %s\n", ts, program, type, message); // out to file though
                if ( AK_DEBUG )
                {
                    asprintf(&some_string, "%s <%s> [%s] %s", ts_string, program, type, message);
                    ak_log_print_log_line(some_string);
                    // fprintf(stderr, "%s <%s> [%s] %s\n", ts_string, program, type, message);
                }
            }
            else
            {
                // echo "$TS" "<$1>" "[ERROR]" "No message" >> $AK_LOGSFILE
                printf("%s <%s> [ERROR] No message\n", ts_string, program); // out to file
                if ( AK_DEBUG )
                {
                    fprintf(stderr, "%s <%s> [ERROR] No message\n", ts_string, program);
                }
                exit(1);
            }
        }
        else
        {
            // echo "$TS" "<$1>" "[ERROR]" "No type and message" >> $AK_LOGSFILE

            printf("%s <%s> [ERROR] No type and message\n", ts_string, program); // out to file
            if ( AK_DEBUG )
            {
                fprintf(stderr, "%s <%s> [ERROR] No type and message\n", ts_string, program);
            }
            exit(1);
        }
    }
    else
    {
        // echo "$TS" "<$(basename $0)>" "[ERROR]" "No arguments given" >> $AK_LOGSFILE
        printf("%s <%s> [ERROR] No arguments given\n", ts_string, program); // out to file
        if ( AK_DEBUG )
        {
            fprintf(stderr, "%s <%s> [ERROR] No arguments given\n", ts_string, program);
        }
        exit(1);
    }
}

void ak_log_exit(char* program, char* message)
{
    ak_log_message(program, "EXIT", message);
}

void ak_log_warning(char* program, char* message)
{
    ak_log_message(program, "WARNING", message);
}

void ak_log_debug(char* program, char* message)
{
    ak_log_message(program, "DEBUG", message);
}

void ak_log_error(char* program, char* message)
{
    ak_log_message(program, "ERROR", message);
}

void ak_log_info(char* program, char* message)
{
    ak_log_message(program, "INFO", message);
}