summaryrefslogtreecommitdiffstats
path: root/src/framework/log/nstack_log_async.c
blob: 1b17d8dd31307cb12456577f42b27748fed48192 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*==============================================*
 *      include header files                    *
 *----------------------------------------------*/

#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "types.h"
#include "nstack_securec.h"
#include "nsfw_base_linux_api.h"
#include "nstack_log_async.h"

#include <signal.h>
#include <syscall.h>

/*sockaddr_un.sun_path is an array of 108 bytes*/
#define UNIX_SOCK_MAX_PATH_LEN 108
#define MAX_CONN_NUM 5
#define MAX_LOG_RECV_BUF         0x34000*2
#define ASYNLOG_THREAD_NAME "nstk_log_asyn"

NSTACK_STATIC char nstack_sock_running[UNIX_SOCK_MAX_PATH_LEN + 1];
NSTACK_STATIC char nstack_sock_operation[UNIX_SOCK_MAX_PATH_LEN + 1];
NSTACK_STATIC char nstack_sock_master[UNIX_SOCK_MAX_PATH_LEN + 1];
NSTACK_STATIC char nstack_sock_ctrl[UNIX_SOCK_MAX_PATH_LEN + 1];
NSTACK_STATIC char nstack_sock_dir[UNIX_SOCK_MAX_PATH_LEN + 1];

int g_nstack_log_client_fd[MAX_LOG_TYPE] = { -1, -1, -1, -1, -1 };
int g_nstack_log_server_fd[MAX_LOG_TYPE] = { -1, -1, -1, -1, -1 };

#define NSTACK_LOG_SER_STATE_RUNNING 0
#define NSTACK_LOG_SER_STATE_FLUSHING 1

#define NSTACK_LOG_SER_FLUSH_SIG '\5'

NSTACK_STATIC int g_nstack_log_server_state[MAX_LOG_TYPE] = { NSTACK_LOG_SER_STATE_RUNNING };   /*can be used */

static int pre_init_log_count = 0;
static struct pre_init_info pre_init_log[MAX_PRE_INIT_LOG_COUNT] =
    { {0, ""} };
__thread unsigned int pre_log_nonreentry = 0;
extern bool log_asyn_inited;

/*****************************************************************************
*   Prototype    : nstack_log_sock_set
*   Description  : set the sockfd state, for example O_NONBLOCK and so on,
*   Input        : int sock: fd.
*                : int type: module type.
*                  ...
*   Output       : None
*   Return Value : 0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
NSTACK_STATIC inline int nstack_log_sock_set(int sock, int type)
{
    int flags;

    if (type < 0)
    {
        return -1;
    }

    flags = nsfw_base_fcntl(sock, F_GETFD, 0);
    if (flags < 0)
    {
        return -1;
    }

    flags |= type;

    if (nsfw_base_fcntl(sock, F_SETFD, flags) < 0)
    {
        return -1;
    }

    return 0;
}

/*****************************************************************************
*   Prototype    : unlink_log_servername
*   Description  : unlink the servername
*   Input        : int log_type:
*                  ...
*   Output       : None
*   Return Value : void
*   Calls        :
*   Called By    :
*****************************************************************************/

void unlink_log_servername(int log_type)
{
    switch (log_type)
    {
        case LOG_TYPE_NSTACK:
            unlink(nstack_sock_running);
            break;
        case LOG_TYPE_OPERATION:
            unlink(nstack_sock_operation);
            break;
        case LOG_TYPE_MASTER:
            unlink(nstack_sock_master);
            break;
        case LOG_TYPE_CTRL:
            unlink(nstack_sock_ctrl);
            break;
        default:
            break;
    }
    return;

}

/*****************************************************************************
*   Prototype    : nstack_log_sock_path
*   Description  : init the nstack log domain socket path which use to handle
*                  the log with a thread of long connect
*   Input        : int proc_type
*                  ...
*   Output       : static varible store
*   Return Value : >=0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
NSTACK_STATIC int nstack_log_sock_path(int proc_type)
{
    char *directory = "/var/run";
    const char *home_dir = getenv("HOME");      /*can be used */
    bool env_path = FALSE;
    int ret = -1;
    int val = -1;
    int val_opera = -1;
    pid_t pid = getpid();

    if (getuid() != 0 && home_dir != NULL)
    {
        directory = realpath(home_dir, NULL);
        if (!directory)
        {
            save_pre_init_log(NSLOG_ERR, "directory is NULL]errno=%d", errno);
            return -1;
        }
        env_path = TRUE;
    }

    if (EOK != (ret = strcpy_s(nstack_sock_dir, UNIX_SOCK_MAX_PATH_LEN, directory)))    /* check return value with EOK */
    {
        save_pre_init_log(NSLOG_ERR, "strcpy_s fail]ret=%d", ret);
        goto err_init;
    }

    /*modify 'destMax' and return value check */
    if (EOK !=
        (ret =
         strcat_s(nstack_sock_dir, sizeof(nstack_sock_dir), "/ip_module")))
    {
        save_pre_init_log(NSLOG_ERR, "strcat_s fail]ret=%d", ret);
        goto err_init;
    }

    switch (proc_type)
    {
        case LOG_PRO_NSTACK:
            val =
                sprintf_s(nstack_sock_running, UNIX_SOCK_MAX_PATH_LEN,
                          "%s/%s_%d", nstack_sock_dir, "nStackMainRunLog",
                          pid);
            val_opera =
                sprintf_s(nstack_sock_operation, UNIX_SOCK_MAX_PATH_LEN,
                          "%s/%s_%d", nstack_sock_dir, "nStackMainOpeLog",
                          pid);
            if (val_opera < 0)
            {
                save_pre_init_log(NSLOG_ERR, "sprintf_s fail]val_opera=%d",
                                  val_opera);
                ret = -1;
                goto err_init;
            }
            break;
        case LOG_PRO_MASTER:
            val =
                sprintf_s(nstack_sock_master, UNIX_SOCK_MAX_PATH_LEN,
                          "%s/%s_%d", nstack_sock_dir, "nStackMasterLog",
                          pid);
            break;
        case LOG_PRO_OMC_CTRL:
            // nStackCtrl don't add pid, as it may exit before unlink which may leave a useless file in ip_module directory.
            // this will cause losing some log sometime, but it's ok.
            val =
                sprintf_s(nstack_sock_ctrl, UNIX_SOCK_MAX_PATH_LEN, "%s/%s",
                          nstack_sock_dir, "nStackCtrlLog");
            break;
        default:
            save_pre_init_log(NSLOG_ERR, "process invalid]proc_type=%d",
                              proc_type);
            ret = -1;
            goto err_init;
    }

    if ((val < 0))
    {
        save_pre_init_log(NSLOG_ERR, "sprintf_s fail]proc_type=%d,val=%d",
                          proc_type, val);
        ret = -1;
        goto err_init;
    }

    ret = 0;

  err_init:

    if (env_path == TRUE)
    {
        free(directory);
    }
    return ret;
}

/*****************************************************************************
*   Prototype    : nstack_log_sock_listen
*   Description  : create the nstack log domain socket, bind the share domain file,
*                  listen the client connect, for server side.
*   Input        : const char *servername, file name for the domain
*                  ...
*   Output       : listen fd
*   Return Value : >=0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
NSTACK_STATIC int nstack_log_sock_listen(const char *servername)
{
    int fd, ret;
    unsigned int len;
    struct stat st;
    struct sockaddr_un un;
    int opt = 1;

    if ((ret = stat(nstack_sock_dir, &st)) != 0)
    {
        save_pre_init_log(NSLOG_ERR,
                          "stat get file info fail]ret=%d,nstack_sock_dir=%s",
                          ret, nstack_sock_dir);
        return -1;
    }

    if ((fd = nsfw_base_socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
    {
        save_pre_init_log(NSLOG_ERR, "create socket fail]fd=%d,errno=%d", fd,
                          errno);
        return -1;
    }

    if ((ret =
         nsfw_base_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt,
                              sizeof(opt))) < 0)
    {
        save_pre_init_log(NSLOG_ERR, "set socket reuse fail]ret=%d,errno=%d",
                          ret, errno);
        goto listen_err;
    }

    unlink(servername);         /* in case it already exists */

    ret = memset_s(&un, sizeof(un), 0, sizeof(un));
    if (EOK != ret)
    {
        save_pre_init_log(NSLOG_ERR, "memset_s fail]ret=%d", ret);
        goto listen_err;
    }

    un.sun_family = AF_UNIX;
    ret = strcpy_s(un.sun_path, sizeof(un.sun_path), servername);
    if (EOK != ret)
    {
        save_pre_init_log(NSLOG_ERR, "strcpy_s fail]ret=%d", ret);
        goto listen_err;
    }

    len =
        (unsigned int) (offsetof(struct sockaddr_un, sun_path) +
                        strlen(servername));

    if ((ret = nsfw_base_bind(fd, (struct sockaddr *) &un, len)) < 0)
    {
        save_pre_init_log(NSLOG_ERR,
                          "bind domain socket fail]ret=%d,errno=%d", ret,
                          errno);
        goto listen_err;
    }

    if ((ret = nsfw_base_listen(fd, MAX_CONN_NUM)) < 0)
    {
        save_pre_init_log(NSLOG_ERR, "listen fail]ret=%d,errno=%d", ret,
                          errno);
        goto listen_err;
    }

    return fd;

  listen_err:

    (void) nsfw_base_close(fd);

    return -1;

}

/*****************************************************************************
*   Prototype    : nstack_log_fd_to_type
*   Description  : traverse the fd from fds to get the fd num, it returns
*                  the file type, which matches the fd.
*   Input        : int *fds
*                : int fd
*                  ...
*   Output       : fd
*   Return Value : >=0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
NSTACK_STATIC inline int nstack_log_fd_to_type(const int *fds, int fd)
{
    int i;
    for (i = 0; i <= LOG_TYPE_CTRL; i++)
    {
        if (fd == fds[i])
        {
            return i;
        }
    }
    return -1;
}

/*****************************************************************************
*   Prototype    : nstack_log_signal_handler
*   Description  : syn Leibniz issue, the signal interrupt 34, the log collect
*                  thread create early as process signal handle too late.
*   Input        : None
*                  ...
*   Output       : None
*   Return Value : 0 success, -1 false
*   Calls        :
*   Called By    :
*****************************************************************************/
int nstack_log_signal_handler()
{
    sigset_t waitset, oset;
    int s = -1;

    if (0 != sigemptyset(&waitset))
    {
        NS_PTHREADLOG(LOG_TYPE_UNRECOGNIZED, NSLOG_ERR, "sigemptyset fail");
        return -1;
    }

    if (0 != sigaddset(&waitset, SIGRTMIN))
    {
        NS_PTHREADLOG(LOG_TYPE_UNRECOGNIZED, NSLOG_ERR,
                      "sigaddset fail]SIGRTMIN");
        return -1;
    }

    if (0 != sigaddset(&waitset, SIGRTMIN + 2))
    {
        NS_PTHREADLOG(LOG_TYPE_UNRECOGNIZED, NSLOG_ERR,
                      "sigaddset fail]SIGRTMIN+2");
        return -1;
    }

    if ((s = pthread_sigmask(SIG_BLOCK, &waitset, &oset)) != 0)
    {
        NS_PTHREADLOG(LOG_TYPE_UNRECOGNIZED, NSLOG_ERR,
                      "pthread_sigmask fail]s=%d", s);
        return -1;
    }

    return 0;

}

#define ASYNC_MAXEVENTS 20
NSTACK_STATIC int nstack_log_server_prepare(int proc_type,
                                            struct epoll_event *ev,
                                            int *epfd,
                                            struct epoll_event *events)
{
    int ret;
    int listen_fd;

    /* thread signal mask handle */
    if (nstack_log_signal_handler() < 0)
    {
        NS_PTHREADLOG(LOG_TYPE_UNRECOGNIZED, NSLOG_ERR,
                      "signal handle fail]proc_type=%d", proc_type);
        return -1;
    }

    /* init the array */
    ret =
        memset_s(events, sizeof(struct epoll_event) * ASYNC_MAXEVENTS, 0,
                 sizeof(struct epoll_event) * ASYNC_MAXEVENTS);
    if (EOK != ret)
    {
        NS_PTHREADLOG(LOG_TYPE_UNRECOGNIZED, NSLOG_ERR,
                      "memset_s fail]ret=%d,proc_type=%d", ret, proc_type);
        return -1;
    }

    // create ep fd, the max scope of epoll is 256
    *epfd = nsfw_base_epoll_create(256);
    if (*epfd < 0)
    {
        NS_PTHREADLOG(LOG_TYPE_UNRECOGNIZED, NSLOG_ERR,
                      "create epoll fail]epfd=%d,proc_type=%d,errno=%d",
                      *epfd, proc_type, errno);
        return -1;
    }

    int i;
    for (i = 0; i <= LOG_TYPE_CTRL; i++)
    {
        if ((listen_fd = g_nstack_log_server_fd[i]) <= 0)
        {
            continue;
        }
        ev->data.fd = listen_fd;
        ev->events = EPOLLIN;

        // add server listen fd to epoll
        if ((ret =
             nsfw_base_epoll_ctl(*epfd, EPOLL_CTL_ADD, listen_fd, ev)) < 0)
        {
            NS_PTHREADLOG(LOG_TYPE_UNRECOGNIZED, NSLOG_ERR,
                          "epoll ctl add fail]ret=%d,epfd=%d,listen_fd=%d,errno=%d",
                          ret, *epfd, listen_fd, errno);
            return -1;
        }
    }
    return 0;

}

NSTACK_STATIC int nstack_log_server_recv_old(int *accepted_fd,
                                             struct epoll_event *event)
{
    int sockfd;
    int log_type;
    //one log content 2048 enough.
    char buffer[MAX_BUFFER_LEN];
    ssize_t num;

    log_type = nstack_log_fd_to_type(accepted_fd, event->data.fd);
    /*log_type has been protected */
    if ((log_type >= 0) && (log_type < MAX_LOG_TYPE))
    {
        if ((sockfd = event->data.fd) < 0)
        {
            NS_PTHREADLOG(log_type, NSLOG_WAR,
                          "accept fd invalid]sockfd=%d,log_type=%d", sockfd,
                          log_type);
            return -1;
        }

        /* change 3th param from 'sizeof(buffer)' to 'sizeof(buffer)-1', make room for '\0' terminated */
        if ((num =
             nsfw_base_recv(sockfd, buffer, sizeof(buffer) - 1, 0)) <= 0)
        {
            if (num == 0 || (errno != EAGAIN && errno != EINTR))
            {
                //if server close, client side will recv EPIPE
                NS_PTHREADLOG(log_type, NSLOG_ERR,
                              "recv the msg fail]sockfd=%d,num=%ld,log_type=%d,errno=%d",
                              sockfd, num, log_type, errno);
                (void) nsfw_base_close(accepted_fd[log_type]);
                accepted_fd[log_type] = -1;
            }
            return -1;
        }

        int offset = 0;
        int j;
        if (g_nstack_log_server_state[log_type] ==
            NSTACK_LOG_SER_STATE_FLUSHING)
        {
            for (j = 0; j < num; j++)
            {
                if (buffer[j] == NSTACK_LOG_SER_FLUSH_SIG)
                {
                    buffer[j] = '\0';
                    glog_print_buffer(log_type, buffer, j);
                    glogFlushLogFiles(GLOG_LEVEL_DEBUG);

                    (void) (__sync_bool_compare_and_swap
                            (&g_nstack_log_server_state[log_type],
                             NSTACK_LOG_SER_STATE_FLUSHING,
                             NSTACK_LOG_SER_STATE_RUNNING));

                    offset = j + 1;
                    break;
                }
            }
        }

        /* make buffer '\0' terminated. glog_print_buffer() need a '\0' terminated string, or it will coredump!!! */
        buffer[num] = '\0';

        if (offset < num)
        {
            // write file
            glog_print_buffer(log_type, buffer + offset, num - offset);
        }

        return -1;
    }

    return 0;
}

NSTACK_STATIC int nstack_log_server_accept_new(int *accepted_fd,
                                               struct epoll_event *ev,
                                               int epfd,
                                               struct epoll_event *event)
{
    int ret;
    int log_type;

    log_type = nstack_log_fd_to_type(g_nstack_log_server_fd, event->data.fd);
    /*log_type has been protected */
    if ((log_type >= 0) && (log_type < MAX_LOG_TYPE))
    {
        struct sockaddr_un un_cli;
        socklen_t clilen = sizeof(un_cli);
        accepted_fd[log_type] =
            nsfw_base_accept(event->data.fd, (struct sockaddr *) &un_cli,
                             &clilen);
        if (accepted_fd[log_type] < 0)
        {
            NS_PTHREADLOG(log_type, NSLOG_WAR,
                          "accept the socket fail]accepted_fd[%d]=%d,errno=%d",
                          log_type, accepted_fd[log_type], errno);
            return -1;
        }
        ev->data.fd = accepted_fd[log_type];

        int size, size_len;
        size = MAX_LOG_RECV_BUF;
        size_len = sizeof(size);
        if ((ret =
             nsfw_base_setsockopt(accepted_fd[log_type], SOL_SOCKET,
                                  SO_RCVBUF, (void *) &size,
                                  (socklen_t) size_len)) < 0)
        {
            NS_PTHREADLOG(log_type, NSLOG_WAR,
                          "set the socket sendbuf fail]accepted_fd[%d]=%d,ret=%d,errno=%d",
                          log_type, accepted_fd[log_type], ret, errno);
            (void) nsfw_base_close(accepted_fd[log_type]);
            accepted_fd[log_type] = -1;
            return -1;
        }

        //set the non_blocking mode.
        if ((ret =
             nstack_log_sock_set(accepted_fd[log_type], O_NONBLOCK)) < 0)
        {
            NS_PTHREADLOG(log_type, NSLOG_WAR,
                          "set the socket non_blocking fail]accepted_fd[%d]=%d,ret=%d",
                          log_type, accepted_fd[log_type], ret);
            (void) nsfw_base_close(accepted_fd[log_type]);
            accepted_fd[log_type] = -1;
            return -1;
        }

        if ((ret =
             nsfw_base_epoll_ctl(epfd, EPOLL_CTL_ADD, accepted_fd[log_type],
                                 ev)) < 0)
        {
            NS_PTHREADLOG(log_type, NSLOG_WAR,
                          "add epoll fail]accepted_fd[%d]=%d,ret=%d,errno=%d",
                          log_type, accepted_fd[log_type], ret, errno);
            (void) nsfw_base_close(accepted_fd[log_type]);
            accepted_fd[log_type] = -1;
            return -1;
        }

        if ((ret =
             nsfw_base_epoll_ctl(epfd, EPOLL_CTL_DEL, event->data.fd,
                                 NULL)) < 0)
        {
            NS_PTHREADLOG(log_type, NSLOG_WAR,
                          "delete the epoll listen fail]event->data.fd=%d,ret=%d,errno=%d",
                          event->data.fd, ret, errno);
        }

        // remove server listening fd and servername as no use then
        (void) nsfw_base_close(event->data.fd);

        unlink_log_servername(log_type);

    }

    return 0;
}

/*****************************************************************************
*   Prototype    : nstack_log_server_process
*   Description  : init the nstack log domain socket for server side, and create
*                  a thread to wait the client conect or data come.
*   Input        : int proc_type
*                  ...
*   Output       : None
*   Return Value : void*
*   Calls        :
*   Called By    :
*****************************************************************************/
void *nstack_log_server_process(void *args)
{
    int epfd;
    struct epoll_event ev, events[ASYNC_MAXEVENTS];
    int accepted_fd[MAX_LOG_TYPE] = { -1, -1, -1, -1, -1 };
    int proc_type = (int) (u64) args;

    if (0 != nstack_log_server_prepare(proc_type, &ev, &epfd, events))
    {
        /* err msg has been printed */
        return ((void *) 0);
    }

    while (1)
    {
        int i;
        int nfds = nsfw_base_epoll_wait(epfd, events, ASYNC_MAXEVENTS, -1);

        for (i = 0; i < nfds; i++)
        {
            /*Notes: it listen the epoll all of the process, cannot save the log by save_pre_init_log
             *       for the log recv module*/
            if (0 != nstack_log_server_recv_old(accepted_fd, &events[i]))
                continue;

            /*Notes: accept the client fd */
            if (0 !=
                nstack_log_server_accept_new(accepted_fd, &ev, epfd,
                                             &events[i]))
                continue;
        }

    }

    return ((void *) 0);        /*can be used, should never get here */
}

/*****************************************************************************
*   Prototype    : nstack_log_sock_conn
*   Description  : create the nstack log domain socket, and connect the server,
*                  for client side.
*   Input        : const char *servername, file name for the domain
*                  ...
*   Output       : fd
*   Return Value : >=0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
NSTACK_STATIC int nstack_log_sock_conn(const char *servername)
{
    int fd = -1;
    unsigned int len;
    struct sockaddr_un un;
    int opt = 1;
    int ret = -1;
    int size = MAX_LOG_RECV_BUF;
    size_t size_len;
    size_len = sizeof(size);

    if (NULL == servername)
    {
        save_pre_init_log(NSLOG_ERR,
                          "invalid input parameter]servername=NULL");
        return -1;
    }

    //create a UNIX domain stream socket and it is non blocking
    if ((fd = nsfw_base_socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)   /*can be used */
    {
        save_pre_init_log(NSLOG_ERR,
                          "client create the domain socket fail]fd=%d,errno=%d",
                          fd, errno);
        return -1;
    }

    if ((ret =
         nsfw_base_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt,
                              sizeof(opt))) < 0)
    {
        save_pre_init_log(NSLOG_ERR,
                          "client set domain socket reuse fail]fd=%d,ret=%d,errno=%d",
                          fd, ret, errno);
        goto connect_err;
    }

    if ((ret =
         nsfw_base_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &size,
                              (socklen_t) size_len)) < 0)
    {
        save_pre_init_log(NSLOG_ERR,
                          "client set domain socket sendbuf fail]fd=%d,ret=%d,errno=%d",
                          fd, ret, errno);
        goto connect_err;
    }

    ret = memset_s(&un, sizeof(un), 0, sizeof(un));
    if (EOK != ret)
    {
        save_pre_init_log(NSLOG_ERR, "memset_s fail]ret=%d", ret);
        goto connect_err;
    }
    un.sun_family = AF_UNIX;
    if (strlen(servername) > UNIX_SOCK_MAX_PATH_LEN)
    {
        save_pre_init_log(NSLOG_ERR,
                          "servername string is %d bigger than %d",
                          strlen(servername), UNIX_SOCK_MAX_PATH_LEN);
        goto connect_err;
    }
    ret = strcpy_s(un.sun_path, sizeof(un.sun_path), servername);
    if (EOK != ret)
    {
        save_pre_init_log(NSLOG_ERR, "strcpy_s fail]ret=%d", ret);
        goto connect_err;
    }

    len =
        (unsigned int) (offsetof(struct sockaddr_un, sun_path) +
                        strlen(servername));

    //connect the server
    if ((ret = nsfw_base_connect(fd, (struct sockaddr *) &un, len)) < 0)
    {
        save_pre_init_log(NSLOG_ERR,
                          "client connect the server fail]ret=%d,errno=%d",
                          ret, errno);
        goto connect_err;
    }

    return fd;

  connect_err:

    (void) nsfw_base_close(fd);

    return -1;

}

/*****************************************************************************
*   Prototype    : nstack_log_server_init
*   Description  : init the nstack log domain socket for server side, and create
*                  a thread to wait the client conect or data come.
*   Input        : int proc_type
*                  ...
*   Output       : None
*   Return Value : 0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
int nstack_log_server_init(int proc_type)
{
    int ret = -1;
    int listen_fd = -1;
    int listen_fd_opera = -1;

    if ((ret = nstack_log_sock_path(proc_type)) < 0)
    {
        save_pre_init_log(NSLOG_ERR, "asyn server get path fail]ret=%d", ret);
        return -1;
    }

    switch (proc_type)
    {
        case LOG_PRO_NSTACK:
            g_nstack_log_server_state[LOG_TYPE_NSTACK] =
                NSTACK_LOG_SER_STATE_RUNNING;
            g_nstack_log_server_state[LOG_TYPE_OPERATION] =
                NSTACK_LOG_SER_STATE_RUNNING;
            listen_fd = nstack_log_sock_listen(nstack_sock_running);
            g_nstack_log_server_fd[LOG_TYPE_NSTACK] = listen_fd;
            listen_fd_opera = nstack_log_sock_listen(nstack_sock_operation);
            g_nstack_log_server_fd[LOG_TYPE_OPERATION] = listen_fd_opera;
            if (listen_fd_opera < 0)
            {
                save_pre_init_log(NSLOG_ERR,
                                  "asyn server operation fd listen fail]listen_fd_opera=%d",
                                  listen_fd_opera);
                return -1;
            }
            break;
        case LOG_PRO_MASTER:
            g_nstack_log_server_state[LOG_TYPE_MASTER] =
                NSTACK_LOG_SER_STATE_RUNNING;
            listen_fd = nstack_log_sock_listen(nstack_sock_master);
            g_nstack_log_server_fd[LOG_TYPE_MASTER] = listen_fd;
            break;
        case LOG_PRO_OMC_CTRL:
            g_nstack_log_server_state[LOG_TYPE_CTRL] =
                NSTACK_LOG_SER_STATE_RUNNING;
            listen_fd = nstack_log_sock_listen(nstack_sock_ctrl);
            g_nstack_log_server_fd[LOG_TYPE_CTRL] = listen_fd;
            break;
        default:
            save_pre_init_log(NSLOG_ERR, "process invalid]proc_type=%d",
                              proc_type);
            return -1;
    }

    if (listen_fd < 0)
    {
        save_pre_init_log(NSLOG_ERR, "asyn server listen fail]listen_fd=%d",
                          listen_fd);
        return -1;
    }

    pthread_t t;
    if ((ret =
         pthread_create(&t, NULL, nstack_log_server_process,
                        (void *) (u64) proc_type)) != 0)
    {
        save_pre_init_log(NSLOG_ERR, "asyn server create thread fail]ret=%d",
                          ret);
        return -1;
    }

    /* thread name string should smaller than 16 bytes */

    ret = pthread_setname_np(t, ASYNLOG_THREAD_NAME);
    if (ret != 0)
    {
        save_pre_init_log(NSLOG_WAR,
                          "asyn server set thread name fail, use process name]ret=%d",
                          ret);
    }
    else
    {
        save_pre_init_log(NSLOG_INF,
                          "asyn server set thread name success]thread name=%s",
                          ASYNLOG_THREAD_NAME);
    }

    if (proc_type == LOG_PRO_NSTACK)
    {
        save_pre_init_log(NSLOG_INF,
                          "asyn server init success]listen_fd=%d,listen_fd_opera=%d",
                          listen_fd, listen_fd_opera);
    }
    else
    {
        save_pre_init_log(NSLOG_INF, "asyn server init success]listen_fd=%d",
                          listen_fd);
    }

    return 0;
}

#define NSTACK_LOG_SEND_FLUSH_SIG(fd, ret) \
do \
{\
    if (fd > 0) \
    { \
        ret = nsfw_base_send(fd, "\5" /* NSTACK_LOG_SER_FLUSH_SIG */, 1, MSG_NOSIGNAL); \
    } else { \
        ret = -1; \
    }\
} while(0)

#ifndef MAX_U64_NUM
#define MAX_U64_NUM ((unsigned long long)0xffffffffffffffff)
#endif

NSTACK_STATIC int nstack_log_current_time2msec(unsigned long long *msec)
{
    struct timespec tout;
    if (0 != clock_gettime(CLOCK_MONOTONIC, &tout))
    {
        return -1;
    }

    if (MAX_U64_NUM / 1000 < (unsigned long long) tout.tv_sec)
    {
        return -1;
    }
    unsigned long long sec2msec = 1000 * tout.tv_sec;
    unsigned long long nsec2msec =
        (unsigned long long) tout.tv_nsec / 1000000;

    if (MAX_U64_NUM - sec2msec < nsec2msec)
    {
        return -1;
    }

    *msec = sec2msec + nsec2msec;
    return 0;
}

int nstack_log_server_flush(int proc_type, unsigned long long timeout)
{
    int i = 0;
    unsigned long long start = 0;
    unsigned long long end = 0;
    int flushed = 0;
    int ret = 0;

    switch (proc_type)
    {
        case LOG_PRO_NSTACK:
            g_nstack_log_server_state[LOG_TYPE_NSTACK] =
                NSTACK_LOG_SER_STATE_FLUSHING;
            g_nstack_log_server_state[LOG_TYPE_OPERATION] =
                NSTACK_LOG_SER_STATE_FLUSHING;

            // send ENQ
            NSTACK_LOG_SEND_FLUSH_SIG(g_nstack_log_client_fd
                                      [LOG_TYPE_NSTACK], ret);
            if (ret < 0)
            {
                g_nstack_log_server_state[LOG_TYPE_NSTACK] =
                    NSTACK_LOG_SER_STATE_RUNNING;
                g_nstack_log_server_state[LOG_TYPE_OPERATION] =
                    NSTACK_LOG_SER_STATE_RUNNING;
                return -1;
            }
            NSTACK_LOG_SEND_FLUSH_SIG(g_nstack_log_client_fd
                                      [LOG_TYPE_OPERATION], ret);
            if (ret < 0)
            {
                g_nstack_log_server_state[LOG_TYPE_OPERATION] =
                    NSTACK_LOG_SER_STATE_RUNNING;
                return -1;
            }
            break;
        case LOG_PRO_MASTER:
            g_nstack_log_server_state[LOG_TYPE_MASTER] =
                NSTACK_LOG_SER_STATE_FLUSHING;
            // send ENQ
            NSTACK_LOG_SEND_FLUSH_SIG(g_nstack_log_client_fd
                                      [LOG_TYPE_MASTER], ret);
            if (ret < 0)
            {
                g_nstack_log_server_state[LOG_TYPE_MASTER] =
                    NSTACK_LOG_SER_STATE_RUNNING;
                return -1;
            }
            break;
        case LOG_PRO_OMC_CTRL:
            g_nstack_log_server_state[LOG_TYPE_CTRL] =
                NSTACK_LOG_SER_STATE_FLUSHING;
            // send ENQ
            NSTACK_LOG_SEND_FLUSH_SIG(g_nstack_log_client_fd[LOG_TYPE_CTRL],
                                      ret);
            if (ret < 0)
            {
                g_nstack_log_server_state[LOG_TYPE_CTRL] =
                    NSTACK_LOG_SER_STATE_RUNNING;
                return -1;
            }
            break;
        default:
            return 0;
    }

    if (nstack_log_current_time2msec(&start))
    {
        return -1;
    }

    while (1)
    {
        if (timeout > 0)
        {
            if (nstack_log_current_time2msec(&end))
            {
                break;
            }
            if (end < start || (end - start) > timeout)
            {
                break;
            }
        }

        flushed = 1;
        for (i = 0; i < MAX_LOG_TYPE; i++)
        {
            if (g_nstack_log_server_state[i] != NSTACK_LOG_SER_STATE_RUNNING)
            {
                flushed = 0;
                break;
            }
        }
        if (flushed)
        {
            return 0;
        }
        sys_sleep_ns(0, 500000);
    }

    return -1;
}

/*****************************************************************************
*   Prototype    : nstack_log_client_init
*   Description  : init the nstack log domain socket, which use to handle
*                  the log with a thread of the domain connect, store the
*                  client fd, for client side.
*   Input        : int proc_type
*                  ...
*   Output       : None
*   Return Value : 0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
int nstack_log_client_init(int proc_type)
{
    int connfd = -1;
    int connfd_opt = -1;

    switch (proc_type)
    {
        case LOG_PRO_NSTACK:
            //get the con fd and store the fd to g_nstack_log_client_fd
            connfd = nstack_log_sock_conn(nstack_sock_running);
            g_nstack_log_client_fd[LOG_TYPE_NSTACK] = connfd;
            connfd_opt = nstack_log_sock_conn(nstack_sock_operation);
            if (connfd_opt < 0)
            {
                save_pre_init_log(NSLOG_ERR,
                                  "asyn client init fail]connfd_opt=%d",
                                  connfd_opt);
                return -1;
            }
            g_nstack_log_client_fd[LOG_TYPE_OPERATION] = connfd_opt;
            break;
        case LOG_PRO_MASTER:
            connfd = nstack_log_sock_conn(nstack_sock_master);
            g_nstack_log_client_fd[LOG_TYPE_MASTER] = connfd;
            break;
        case LOG_PRO_OMC_CTRL:
            connfd = nstack_log_sock_conn(nstack_sock_ctrl);
            g_nstack_log_client_fd[LOG_TYPE_CTRL] = connfd;
            break;
        default:
            save_pre_init_log(NSLOG_ERR, "process invalid]proc_type=%d",
                              proc_type);
            return -1;
    }

    if (connfd < 0)
    {
        save_pre_init_log(NSLOG_ERR, "asyn client init fail]connfd=%d",
                          connfd);
        return -1;
    }

    if (proc_type == LOG_PRO_NSTACK)
    {
        save_pre_init_log(NSLOG_INF,
                          "asyn client init success]connfd=%d,connfd_opt=%d",
                          connfd, connfd_opt);
    }
    else
    {
        save_pre_init_log(NSLOG_INF, "asyn client init success]connfd=%d",
                          connfd);
    }

    return 0;
}

/*****************************************************************************
*   Prototype    : nstack_log_client_send
*   Description  : send the log data to the file, file_type will get the fd
*                  for the specified conn.
*   Input        : int file_type
*                : char *buffer
*                : size_t buflen
*                  ...
*   Output       : None
*   Return Value : 0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
int nstack_log_client_send(int file_type, char *buffer, size_t buflen)
{
    int connfd = -1;
    int num = -1;
    int count = 2;              //if errno is EAGAIN or EINTR, try twice
    if ((NULL == buffer) || (buflen == 0) || (file_type < 0)
        || (file_type > LOG_TYPE_CTRL))
    {
        return -1;
    }

    //get the fd as the file_type specified.
    connfd = g_nstack_log_client_fd[file_type];
    if (connfd < 0)
    {
        NS_PTHREADLOG(file_type, NSLOG_ERR,
                      "connfd invalid]connfd=%d,file_type=%d", connfd,
                      file_type);
        goto async_err;
    }

    while (count-- > 0)
    {
        // write to client fd
        if ((num =
             nsfw_base_send(connfd, buffer, buflen, MSG_NOSIGNAL)) == buflen)
        {
            break;
        }
        else if (num <= 0)
        {
            if (num == 0 || ((errno != EAGAIN) && (errno != EINTR)))
            {
                NS_PTHREADLOG(file_type, NSLOG_ERR,
                              "async log module fail, domain socket close]g_nstack_log_client_fd[%d]=%d,num=%d,errno=%d",
                              file_type, connfd, num, errno);
                (void) nsfw_base_close(connfd);
                g_nstack_log_client_fd[file_type] = -1;
                goto async_err;
            }
        }
        else
        {
            break;
        }
    }

    return 0;

  async_err:
    NS_PTHREADLOG(file_type, NSLOG_ERR,
                  "current process will turn DIO synchron log module]buflen=%zu,buffer=%s",
                  buflen, buffer);
    log_asyn_inited = FALSE;

    return -1;
}

/*****************************************************************************
*   Prototype    : nstack_log_level_valid
*   Description  : check if the level is valid.
*   Input        : uint32_t level
*                  ...
*   Output       : None
*   Return Value : TRUE means success, FALSE means fail
*   Calls        :
*   Called By    :
*****************************************************************************/
bool nstack_log_level_valid(uint32_t level)
{
    switch (level)
    {
        case NSLOG_CUS:
        case NSLOG_DBG:
        case NSLOG_INF:
        case NSLOG_WAR:
        case NSLOG_ERR:
        case NSLOG_EMG:
            break;
        default:
            return FALSE;
    }
    return TRUE;
}

/*****************************************************************************
*   Prototype    : save_pre_init_log
*   Description  : save the pre init log for nstack as the log module still
*                  be unavailable.
*   Input        : uint32_t level
*                : char *fmt
*                  ...
*   Output       : None
*   Return Value : void
*   Calls        :
*   Called By    :
*****************************************************************************/

/* change the print level, not only has err */
void save_pre_init_log(uint32_t level, char *fmt, ...)
{
    va_list ap;
    int ret = 0;
    /*add pre_init_log_count rang check */
    if (!nstack_log_level_valid(level)
        || pre_init_log_count >= MAX_PRE_INIT_LOG_COUNT
        || pre_init_log_count < 0)
    {
        return;
    }

    pre_init_log[pre_init_log_count].log_buffer[PRE_INIT_LOG_LENGTH - 1] =
        '\0';

    (void) va_start(ap, fmt);   /*keep behavior same with C00,and it won't any effect here */
    ret =
        vsnprintf_s(pre_init_log[pre_init_log_count].log_buffer,
                    PRE_INIT_LOG_LENGTH, PRE_INIT_LOG_LENGTH - 1, fmt, ap);
    if (-1 == ret)
    {
        va_end(ap);
        return;
    }
    va_end(ap);
    pre_init_log[pre_init_log_count].level = level;
    pre_init_log_count++;
    return;
}

/*****************************************************************************
*   Prototype    : get_pre_init_log_count
*   Description  : get the count value of the pre log record.
*   Input        : None
*                  ...
*   Output       : log count num
*   Return Value : nonnegative number
*   Calls        :
*   Called By    :
*****************************************************************************/
int get_pre_init_log_count()
{
    int count = pre_init_log_count;
    if ((count < 0) || (count >= MAX_PRE_INIT_LOG_COUNT))
    {
        return 0;
    }
    return count;
}

/*****************************************************************************
*   Prototype    : get_pre_init_log_buffer
*   Description  : get the stored log content of the pre log, the content will
*                  copy to the input parameter array for print.
*   Input        : struct pre_init_info pre_buf[]
*                : uint32_t size
*                  ...
*   Output       : None
*   Return Value : 0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
int get_pre_init_log_buffer(struct pre_init_info *pre_buf, uint32_t size)
{
    int ret = -1;
    size_t array_size = sizeof(struct pre_init_info) * size;
    if (NULL == pre_buf || size > MAX_PRE_INIT_LOG_COUNT
        || (sizeof(pre_init_log) != array_size))
    {
        return -1;
    }

    ret = memcpy_s(pre_buf, array_size, pre_init_log, array_size);
    if (EOK != ret)
    {
        return -1;
    }
    return 0;
}

/*****************************************************************************
*   Prototype    : get_level_desc
*   Description  : get the first letter as the level.
*   Input        : uint32_t level
*                  ...
*   Output       : first letter of the level
*   Return Value : char *
*   Calls        :
*   Called By    :
*****************************************************************************/
char *get_level_desc(uint32_t level)
{
    switch (level)
    {
        case NSLOG_DBG:
            return "D";
        case NSLOG_INF:
            return "I";
        case NSLOG_WAR:
            return "W";
        case NSLOG_ERR:
            return "E";
        case NSLOG_EMG:
            /* PDT use fatal, so here use F */
            return "F";
        default:
            return "E";
    }
}

/*****************************************************************************
*   Prototype    : nstack_log_get_prefix
*   Description  : assemble the log prefix content, the content contain the level
*                  first letter, and timestamp
*   Input        : uint32_t level
*                : char *buffer
*                : uint32_t length
*                  ...
*   Output       : buffer store the pre_log
*   Return Value : >=0 means success, -1 means fail.
*   Calls        :
*   Called By    :
*****************************************************************************/
int nstack_log_get_prefix(uint32_t level, char *buffer, uint32_t length)
{
    if ((NULL == buffer) || length == 0)
    {
        return -1;
    }

    int ret = -1;
    char *level_str = "E";
    struct timeval t_val;
    struct tm now_time;

    /* limit log file size and log file count- Begin */
    /*gettimeofday is not change to clock_gettime as this is for log and gettimeofday only
       makes sense here */
    (void) gettimeofday(&t_val, NULL);
    time_t t_sec = (time_t) t_val.tv_sec;
    (void) gmtime_r(&t_sec, &now_time);

    level_str = get_level_desc(level);

    /* There are some unsafe function ,need to be replace with safe function */
    /* modify %02d:%02d:%02d:%06ld:%s to "%02d:%02d:%02d.%06ld %s" */
    ret = sprintf_s(buffer, length, "%s%02d%02d %02d:%02d:%02d.%06ld",
                    level_str, now_time.tm_mon + 1, now_time.tm_mday,
                    now_time.tm_hour, now_time.tm_min, now_time.tm_sec,
                    (long) t_val.tv_usec);
    if (-1 == ret)
    {
        return -1;
    }

    return ret;

}

/*****************************************************************************
*   Prototype    : nstack_log_print_buffer
*   Description  : get the log prefix content, and assemble the log with format,
*                  then use glog print direct, as in a single thread, no block.
*   Input        : uint32_t file_type
*                : uint32_t level
*                : const char *format
*                  ...
*   Output       : NA
*   Return Value : void
*   Calls        :
*   Called By    : nstack_log_server_process
*****************************************************************************/
void nstack_log_print_buffer(uint32_t log_type, uint32_t level,
                             const char *format, ...)
{
    if (NULL == format)
    {
        return;
    }

    va_list ap;
    int ret;
    char pre_buffer[PRE_INIT_LOG_LENGTH] = { 0 };
    char buffer[MAX_BUFFER_LEN] = { 0 };
    char format_buffer[MAX_BUFFER_LEN] = { 0 };

    ret = nstack_log_get_prefix(level, pre_buffer, sizeof(pre_buffer));
    if (ret < 0)
    {
        return;
    }

    ret =
        sprintf_s(format_buffer, sizeof(format_buffer), "%s %s", pre_buffer,
                  format);
    if (ret < 0)
    {
        return;
    }

    va_start(ap, format);       /*no need to check return */
    ret = vsprintf_s(buffer, sizeof(buffer), format_buffer, ap);
    if (-1 == ret)
    {
        va_end(ap);
        return;
    }
    va_end(ap);

    buffer[sizeof(buffer) - 1] = '\0';

    // print the buf
    glog_print_buffer(log_type, buffer, ret);

    return;
}