summaryrefslogtreecommitdiffstats
path: root/logger_service/server/src/ss_logger_util.cpp
blob: 6ac72251f17fdc4206ff0f6ffc30daa89b22e93a (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
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * 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.
 */

///////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_SS_LoggerService
/// \brief    Provide support for SS Logger module utility functions.
///
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// INCLUDES
///////////////////////////////////////////////////////////////////////////////
#include "ss_logger_util.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stub/Clock_API.h>
#include <iomanip>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include "ss_logger_common.h"

#include <system_service/ss_logger_store_logs.h>

#define SS_STORE_FILENAME_SYSILG_TERM "_SYS_ILG_RESET.tar.gz"
#define SS_STORE_FILENAME__CWORD52__TERM "__CWORD52_.log"


char CLoggerUtil::m_usbpath[USB_PATH_SIZE];

///////////////////////////////////////////////////////////////////////////////
// CLASS METHODS
///////////////////////////////////////////////////////////////////////////////
CLoggerUtil::CLoggerUtil(void)
    : m_pCfg(NULL),
      m_Milage(0),
      m_bootCount(0) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  m_usbpath[0] = '\0';
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
}

CLoggerUtil::~CLoggerUtil(void) {  // LCOV_EXCL_START 14:globle instance
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
}
// LCOV_EXCL_STOP

EFrameworkunifiedStatus CLoggerUtil::Initialize(CLoggerCfg *f_pLoggerCfg) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus;

  m_pCfg = f_pLoggerCfg;
  l_eStatus = (NULL != f_pLoggerCfg) ? eFrameworkunifiedStatusOK : eFrameworkunifiedStatusInvldParam;

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}

std::string CLoggerUtil::getDestination(TLoggerErrorEvent &f_eventNtfData) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+: %d ", f_eventNtfData.EventType);
  std::string l_path = "";
  EFrameworkunifiedStatus l_eStatus;

  switch (f_eventNtfData.EventType) {
    case eErrorEventTypeProcessCrash:
    case eErrorEventTypeHeartBeatFailure:
    case eErrorEventTypeSystemLowMemory:
    case eErrorEventTypeBootMicroReset:
    case eErrorEventTypeProcessExit:
    case eErrorEventTypeUserInvokedUserForceReset:
    case eErrorEventTypeModConnFailed:
    case eErrorEventTypeStartRespFailed:
    case eErrorEventTypeUserInvokedCollectDevLogs:
    case eErrorEventTypeModuleInvokedResetRequest:
    case eErrorEventTypeModuleInvokedCollectDebugLogs:
    case eErrorEventTypeDtcEvent:
    case eErrorEventTypeUserInvokedClearLogs:
    case eErrorEventTypeGroupRelaunch:
      l_eStatus = m_pCfg->GetEmmcDestination(l_path);
      break;

    case eErrorEventTypeUserInvokedCollectNaviLog:
      l_eStatus = m_pCfg->GetEmmcNaviLogDestination(l_path);
      break;

    case eErrorEventTypeEelExport:
      l_path = f_eventNtfData.ModuleName;
      l_eStatus = eFrameworkunifiedStatusOK;
      break;

    case eErrorEventTypeInterfaceunifiedEmmcLogs:
      l_path = f_eventNtfData.ModuleName;
      l_eStatus = eFrameworkunifiedStatusOK;
      break;

    case eErrorEventTypeUserInvokedCollectAllLogs:
    case eErrorEventTypeUserInvokedCollectScreenShot:
    case eErrorEventTypeUserInvokedCollectInterfaceunifiedLogs:
    case eErrorEventTypeCanEvent:
      l_eStatus = m_pCfg->GetUserInvokedDestination(l_path);
      break;

    case eErrorEventTypeDiagEvent:
      l_path = f_eventNtfData.ModuleName;
      l_eStatus = eFrameworkunifiedStatusOK;
      break;

    default:
      l_eStatus = eFrameworkunifiedStatusFail;
      break;
  }
  if (eFrameworkunifiedStatusOK != l_eStatus) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. Could not get filepath ");
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %s", l_path.c_str());
  return (l_path);
}
EFrameworkunifiedStatus CLoggerUtil::checkDestinationAvailable(
    TLoggerErrorEvent &f_eventNtfData) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
  std::string l_path = CLoggerUtil::getDestination(f_eventNtfData);
  std::string::size_type ret;

  if (l_path.length() > 0) {
    ret = l_path.find(DEBUG_USB_PATH);
    if (ret != std::string::npos && ret == 0) {
      if (0 == access(m_usbpath, W_OK)) {  // LCOV_EXCL_BR_LINE 5:C code
        // LCOV_EXCL_START 5:C code
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        std::string path = l_path.substr(sizeof(DEBUG_USB_PATH) - 1);
        f_eventNtfData.ModuleName = m_usbpath;
        f_eventNtfData.ModuleName.append(path);
        l_eStatus = eFrameworkunifiedStatusOK;
        // LCOV_EXCL_STOP
      }
    } else {
      if (0 == access(l_path.c_str(), W_OK)) {  // LCOV_EXCL_BR_LINE 5:C code
        l_eStatus = eFrameworkunifiedStatusOK;
      }
    }
  }

  if (eFrameworkunifiedStatusOK != l_eStatus) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           " Error. Failed the check path: l_eStatus(%d), l_path(%s)",
           l_eStatus, l_path.c_str());  // LCOV_EXCL_BR_LINE 11:except,C++ STL
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}

static void SetSuffixFromType(std::string &pathAndName, EErrorEventType type) {  // NOLINT (runtime/references)
  switch (type) {
    case eErrorEventTypeProcessCrash:
    case eErrorEventTypeHeartBeatFailure:
    case eErrorEventTypeSystemLowMemory:
    case eErrorEventTypeBootMicroReset:
    case eErrorEventTypeProcessExit:
    case eErrorEventTypeModConnFailed:
    case eErrorEventTypeStartRespFailed:
    case eErrorEventTypeModuleInvokedResetRequest:
      pathAndName.insert(pathAndName.find(".tar.gz"), "_ERR");
      break;
    case eErrorEventTypeUserInvokedUserForceReset:
    case eErrorEventTypeUserInvokedCollectAllLogs:
    case eErrorEventTypeUserInvokedCollectScreenShot:
    case eErrorEventTypeUserInvokedCollectInterfaceunifiedLogs:
    case eErrorEventTypeDiagEvent:
    case eErrorEventTypeCanEvent:
    case eErrorEventTypeDtcEvent:
    case eErrorEventTypeModuleInvokedCollectDebugLogs:
      pathAndName.insert(pathAndName.find(".tar.gz"), "_DIAG");
      break;
    case eErrorEventTypeUserInvokedCollectDevLogs:
      pathAndName.insert(pathAndName.find(".tar.gz"), "_DEBUG");
      break;
    case eErrorEventTypeGroupRelaunch:
      pathAndName.insert(pathAndName.find(".tar.gz"), "_GRP_RELAUNCH");
      break;
    default:
      break;
  }
}

EFrameworkunifiedStatus CLoggerUtil::getFilePathAndName(HANDLE f_hApp,
                                           TLoggerErrorEvent &f_eventNtfData,
                                           uint32_t f_time,
                                           std::string &f_pathAndName) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  f_pathAndName = "";

  switch (f_eventNtfData.EventType) {  // LCOV_EXCL_BR_LINE 200: eErrorEventTypeEelExport ex. can not be run
    case eErrorEventTypeProcessCrash:
    case eErrorEventTypeProcessExit:
    case eErrorEventTypeHeartBeatFailure:
    case eErrorEventTypeSystemLowMemory:
    case eErrorEventTypeBootMicroReset:
    case eErrorEventTypeModConnFailed:
    case eErrorEventTypeStartRespFailed:
    case eErrorEventTypeGroupRelaunch:
      l_eStatus = m_pCfg->GetEmmcDestination(f_pathAndName);
      if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6:it will aways return ok
        // LCOV_EXCL_START 6:it will aways return ok
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               " Error. GetEmmcDestination() returned %d.",
               l_eStatus);
        // LCOV_EXCL_STOP
      } else {
        f_pathAndName.append("/");
        f_pathAndName.append(GetEmmcFilename(f_time));
      }
      break;

    case eErrorEventTypeUserInvokedUserForceReset:
    case eErrorEventTypeUserInvokedCollectDevLogs:
      l_eStatus = m_pCfg->GetEmmcDestination(f_pathAndName);
      if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6:it will aways return ok
        // LCOV_EXCL_START 6:it will aways return ok
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               " Error. GetEmmcDestination() returned %d.",
               l_eStatus);
        // LCOV_EXCL_STOP
      } else {
        f_pathAndName.append("/");
        f_pathAndName.append(GetEmmcFilename(f_time));
      }
      break;
    case eErrorEventTypeModuleInvokedResetRequest:
    case eErrorEventTypeModuleInvokedCollectDebugLogs:
    case eErrorEventTypeDtcEvent:
      l_eStatus = m_pCfg->GetEmmcDestination(f_pathAndName);
      if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6:it will aways return ok
        // LCOV_EXCL_START 6:it will aways return ok
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               " Error. GetEmmcDestination() returned %d.",
               l_eStatus);
        // LCOV_EXCL_STOP
      } else {
        f_pathAndName.append("/");
        f_pathAndName.append(GetEmmcFilename(f_time));
        if (!f_eventNtfData.ModuleName.empty()) {  // LCOV_EXCL_BR_LINE 6: ModuleName can not be empty
          size_t l_pos = f_pathAndName.find(".tar.gz");
          if (l_pos != std::string::npos) {  // LCOV_EXCL_BR_LINE 6: ".tar.gz" is aways exist
            f_pathAndName.insert(l_pos, f_eventNtfData.ModuleName);
          }
        }
      }
      break;
    case eErrorEventTypeEelExport:  // LCOV_EXCL_START 6: impossible to confirm because it can not get in
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      f_pathAndName = f_eventNtfData.ModuleName;
      f_pathAndName.append("/");
      f_pathAndName.append("loggerservicelogs_export_");
      f_pathAndName.append(GetTimeString(f_time));
      f_pathAndName.append(".tar.gz");
      break;
      // LCOV_EXCL_STOP
    case eErrorEventTypeInterfaceunifiedEmmcLogs:  // LCOV_EXCL_START 6: impossible to confirm because it can not get in
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      // store the target location on event trigger
      f_pathAndName = f_eventNtfData.ModuleName;
      break;
      // LCOV_EXCL_STOP
    case eErrorEventTypeUserInvokedCollectAllLogs:
      l_eStatus = m_pCfg->GetUserInvokedDestination(f_pathAndName);
      if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6: impossible to confirm because it can not be fail
        // LCOV_EXCL_START 6: impossible to confirm because it can not be fail
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               " Error. GetUserInvokedDestination() returned %d.",
               l_eStatus);
        // LCOV_EXCL_STOP
      } else {
        std::string l_emmc_path = m_pCfg->getEmmcOutputPath();
        f_pathAndName.append(l_emmc_path);
        f_pathAndName.append("/");
        f_pathAndName.append(GetUserInvokedFilename(f_hApp, f_time));
        if (!f_eventNtfData.ModuleName.empty()) {  // LCOV_EXCL_BR_LINE 6: it can not be empty
          size_t l_pos = f_pathAndName.find(".tar.gz");
          if (l_pos != std::string::npos) {  // LCOV_EXCL_BR_LINE 6: ".tar.gz" is aways exist
            f_pathAndName.insert(l_pos, f_eventNtfData.ModuleName);
          }
        }
      }
      break;

    case eErrorEventTypeUserInvokedCollectScreenShot:
    case eErrorEventTypeUserInvokedCollectInterfaceunifiedLogs:
    case eErrorEventTypeCanEvent:
      l_eStatus = m_pCfg->GetUserInvokedDestination(f_pathAndName);
      if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6: impossible to confirm because it can not be fail
        // LCOV_EXCL_START 6: impossible to confirm because it can not be fail
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               " Error. GetUserInvokedDestination() returned %d.",
               l_eStatus);
        // LCOV_EXCL_STOP
      } else {
        f_pathAndName.append("/");
        f_pathAndName.append(GetUserInvokedFilename(f_hApp, f_time));
      }
      break;

    case eErrorEventTypeDiagEvent:  // LCOV_EXCL_START 6: impossible to confirm because it can not get in
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      f_pathAndName = f_eventNtfData.ModuleName;
      f_pathAndName.append("/");
      f_pathAndName.append(GetUserInvokedFilename(f_hApp, f_time));
      l_eStatus = eFrameworkunifiedStatusOK;
      break;
      // LCOV_EXCL_STOP
    case eErrorEventTypeUserInvokedClearLogs:
    case eErrorEventTypeUserInvokedCollectNaviLog:
      l_eStatus = eFrameworkunifiedStatusOK;
      break;

    default:
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. Unknown event type: %d.",
             f_eventNtfData.EventType);
      l_eStatus = eFrameworkunifiedStatusFail;
      break;
  }

  SetSuffixFromType(f_pathAndName, f_eventNtfData.EventType);

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}

// LCOV_EXCL_START 13:
EFrameworkunifiedStatus CLoggerUtil::getNaviLogFilePathAndName(std::string &f_prefix,
                                                  uint32_t f_time,
                                                  std::string &f_pathAndName) {  // LCOV_EXCL_BR_LINE 13:
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  l_eStatus = m_pCfg->GetEmmcNaviLogDestination(f_pathAndName);
  if (eFrameworkunifiedStatusOK != l_eStatus) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           " Error. GetEmmNaviLogDestination() returned %d.",
           l_eStatus);
  } else {
    f_pathAndName.append("/");
    f_pathAndName.append(f_prefix);
    f_pathAndName.append(GetEmmcNaviLogFilename(f_time));
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 200:
EFrameworkunifiedStatus CLoggerUtil::getEmmcNaviLogParams(
    uint32_t f_time, EPWR_SHUTDOWN_TRIGGER_TYPE errorType,
    std::string &f_pathName, UI_32 &f_logMax) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  DIR *l_dirp = NULL;
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  std::string l_prefix = "";

  std::vector<std::string> l_vector;
  std::string l_path = m_pCfg->getEmmcOutputNaviLogPath();

  if ((errorType == epssdmsdtGENERIC_ERROR_RESET)
      || (errorType == epssdmsdtFATAL_ERROR_RESET)) {
    /*When reset occured !!*/
    f_logMax = m_pCfg->m_emmcOutputNaviLogErrMax;
    l_prefix = "ERR_";
  } else {
    /* ACC OFF occured*/
    f_logMax = m_pCfg->m_emmcOutputNaviLogNmlMax;
    l_prefix = "NML_";
  }

  if (f_logMax == 0) {
    l_eStatus = eFrameworkunifiedStatusOK;
    return l_eStatus;
  }

  l_dirp = opendir(l_path.c_str());
  if (l_dirp != NULL) {
    struct dirent l_dirent;
    struct dirent* next;

    while (0 == readdir_r(l_dirp, &l_dirent, &next) && next != NULL) {
      std::string l_findFileName = l_dirent.d_name;
      size_t l_find_pos_top = l_findFileName.find(l_prefix.c_str());
      size_t l_find_pos = l_findFileName.find(".log");
      if (std::string::npos != l_find_pos
          && std::string::npos != l_find_pos_top) {
        l_vector.push_back(l_findFileName);
      }
    }
    closedir(l_dirp);
  }
  std::sort(l_vector.begin(), l_vector.end());

  for (UI_32 i = static_cast<UI_32>(l_vector.size()); i != 0; i--) {
    std::string l_FilePath = l_path;
    std::string l_FileName = l_vector.at(i - 1);
    l_FilePath.append("/");
    l_FilePath.append(l_FileName.c_str());

    if (i < f_logMax) {
      size_t l_pos = l_FileName.find("_", l_prefix.size());
      if (std::string::npos != l_pos) {
        std::string l_renameFilePath = l_path;
        std::stringstream l_numStream;
        l_numStream << std::setw(5) << std::setfill('0') << std::right << std::dec << (i + 1);
        l_renameFilePath.append("/");
        l_renameFilePath.append(l_prefix);
        l_renameFilePath.append(l_numStream.str());
        l_renameFilePath.append(l_FileName, l_pos, (l_FileName.size() - l_pos));
        if (0 != rename(l_FilePath.c_str(), l_renameFilePath.c_str())) {
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. rename navilog file %s:%d.",
                 l_FilePath.c_str(), errno);
        }
      }
    } else {
      if (0 != unlink(l_FilePath.c_str())) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. delete navilog file %s:%d.",
               l_FilePath.c_str(), errno);
      }
    }
  }
  // sync parent directory
  SyncDir(l_path);

  l_eStatus = getNaviLogFilePathAndName(l_prefix, f_time, f_pathName);
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 200: can not be called
size_t CLoggerUtil::deleteRequestLogs(std::string f_emmcpath,
                                      ELOGGERSERVICELOGTYPE f_reqtype, size_t f_reqsize,
                                      ELOGGERSERVICELOGTYPE f_deltype) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  DIR* l_dirp = NULL;
  std::vector<std::string> vect;
  std::vector<std::string>::iterator it;
  UI_32 l_count = 0;
  size_t l_totalsize = 0;

  // create saved file list on eMMC
  l_dirp = opendir(f_emmcpath.c_str());
  if (l_dirp != NULL) {
    struct dirent l_dirent;
    struct dirent* next;
    while (0 == readdir_r(l_dirp, &l_dirent, &next) && next != NULL) {
      std::string l_filename = l_dirent.d_name;
      if (CLoggerUtil::QueryLoggerservicelogType(l_filename) == f_deltype) {
        vect.push_back(l_filename);
      }
    }
    closedir(l_dirp);
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " open failed eMMC path: %s",
           f_emmcpath.c_str());
    FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
    return 0;
  }

  // get num of delete logs
  std::sort(vect.begin(), vect.end());
  for (it = vect.begin(); it != vect.end(); it++) {
    l_count++;

    std::string l_filename = *it;
    struct stat st_buf;
    std::string l_tmpstr = f_emmcpath;
    l_tmpstr.append(l_filename.c_str());
    if (stat(l_tmpstr.c_str(), &st_buf) != -1) {
      l_totalsize += st_buf.st_size;
    }

    if (l_totalsize > f_reqsize) {
      break;
    }
  }

  // start delete logs
  if ((l_totalsize > f_reqsize) || (f_reqtype == eLoggerservicelogTypeAbnormal)
      || (f_reqtype == eLoggerservicelogTypeGrpRelaunch)) {
    for (it = vect.begin(); it != vect.end() && l_count > 0; it++) {
      l_count--;

      std::string l_filename = *it;
      std::string l_tmpstr = f_emmcpath;
      l_tmpstr.append(l_filename.c_str());
      if (unlink(l_tmpstr.c_str()) != 0) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " unlink failed %s:%d.",
               l_tmpstr.c_str(), errno);
      }
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " del-file=%s total=%d", l_tmpstr.c_str(),
             l_totalsize);
    }
    SyncDir(f_emmcpath);
  } else {
    l_totalsize = 0;
    FRAMEWORKUNIFIEDLOG(
        ZONE_INFO,
        __FUNCTION__,
        " nothing to delete logs req-type=%d del-logtype=%d total=%d, log_count=%d",
        f_reqtype, f_deltype, l_totalsize, l_count);
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_totalsize;
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 200: can not be called
EFrameworkunifiedStatus CLoggerUtil::forceDeleteOldLog(std::string f_archive_destination,
                                          size_t f_req_size) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
  std::string l_emmc_path;
  size_t l_delsize = 0;
  ELOGGERSERVICELOGTYPE l_logtype = eLoggerservicelogTypeOther;

  size_t l_pos = f_archive_destination.find_last_of('/');
  if (std::string::npos != l_pos) {
    l_emmc_path = f_archive_destination.substr(0, l_pos);
    l_emmc_path.append("/");
    l_logtype = CLoggerUtil::QueryLoggerservicelogType(
        f_archive_destination.substr(l_pos + 1,
                                     f_archive_destination.size() - l_pos + 1));
  }

  if (l_logtype == eLoggerservicelogTypeOther) {
    // not found
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Invalid Parameter. dest=%s",
           f_archive_destination.c_str());
    FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
    return eFrameworkunifiedStatusFail;
  }

  // remove dirty-file if exist
  if (unlink(f_archive_destination.c_str()) != 0) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " unlink failed %s:%d.",
           f_archive_destination.c_str(), errno);
  }

  // delete normal log
  l_delsize = deleteRequestLogs(l_emmc_path, l_logtype, f_req_size,
                                eLoggerservicelogTypeNormal);
  if (l_delsize >= f_req_size) {
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
           " delete normal logs success: req-size=%d, del-size=%d", f_req_size,
           l_delsize);
    l_eStatus = eFrameworkunifiedStatusOK;
  } else if (l_logtype == eLoggerservicelogTypeAbnormal) {
  // delete abnormal log if request type is abnormal
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
           " continue delete abnormal logs: req-size=%d, del-size=%d",
           f_req_size, l_delsize);
    size_t l_tmpsize = f_req_size;
    l_tmpsize -= l_delsize;
    l_delsize = deleteRequestLogs(l_emmc_path, l_logtype, l_tmpsize,
                                  eLoggerservicelogTypeAbnormal);
    if (l_delsize >= l_tmpsize) {
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
             " delete abnormal logs success: req-size=%d, del-size=%d",
             f_req_size, l_delsize);
      l_eStatus = eFrameworkunifiedStatusOK;
    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             " abnormal log delete not complete. req-size=%d, del-size=%d",
             f_req_size, l_delsize);
    }
  } else if (l_logtype == eLoggerservicelogTypeGrpRelaunch) {
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
           " continue delete group relaunch logs: req-size=%d, del-size=%d",
           f_req_size, l_delsize);
    size_t l_tmpsize = f_req_size;
    l_tmpsize -= l_delsize;
    l_delsize = deleteRequestLogs(l_emmc_path, l_logtype, l_tmpsize,
                                  eLoggerservicelogTypeGrpRelaunch);
    if (l_delsize >= l_tmpsize) {
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
             " delete group relaunch logs success: req-size=%d, del-size=%d",
             f_req_size, l_delsize);
      l_eStatus = eFrameworkunifiedStatusOK;
    } else {
      FRAMEWORKUNIFIEDLOG(
          ZONE_ERR, __FUNCTION__,
          " group relaunch log delete not complete. req-size=%d, del-size=%d",
          f_req_size, l_delsize);
    }
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           " normal log del not complete. req-size=%d, del-size=%d", f_req_size,
           l_delsize);
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}
// LCOV_EXCL_STOP

EFrameworkunifiedStatus CLoggerUtil::deleteOldLog(std::string log_path,
                                     std::vector<std::string>& l_vector,
                                     std::string& f_archive_destination,
                                     UI_32 max_num) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  size_t l_pos = f_archive_destination.find_last_of('/');
  if (std::string::npos != l_pos) {  // LCOV_EXCL_BR_LINE 200: there is always '/' in f_destination
    std::sort(l_vector.begin(), l_vector.end());
    std::vector<std::string>::iterator itr = l_vector.begin();
    for (UI_32 i = static_cast<UI_32>(l_vector.size()); (max_num - 1) < i; i--, itr++) {
      if (itr == l_vector.end()) {  // LCOV_EXCL_BR_LINE 200: (max_num - 1) can not be -1.
        break;
      }
      std::string l_FilePath = log_path;
      std::string l_FileName = *itr;
      l_FilePath.append("/");
      l_FilePath.append(l_FileName.c_str());
      if (0 != unlink(l_FilePath.c_str())) {
        l_eStatus = eFrameworkunifiedStatusAccessError;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. delete old log file %s:%d.",
               l_FilePath.c_str(), errno);
      }
    }
    SyncDir(log_path);
  } else {
    // LCOV_EXCL_START 200: there is always '/' in f_destination
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    l_eStatus = eFrameworkunifiedStatusFail;
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. delete target not found. %s",
           f_archive_destination.c_str());
    // LCOV_EXCL_STOP
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

EFrameworkunifiedStatus CLoggerUtil::deleteOldEmmcLog(std::string& f_archive_destination,
                                         EErrorEventType type) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  DIR *l_dirp = NULL;
  std::vector<std::string> vect_abnrm; /* ILLEGAL RESET LOG INFO or ERROR EXIT LOG INFO */
  std::vector<std::string> vect_nrm; /* NORMAL LOG INFO */
  std::vector<std::string> vect_rlnc; /* RELAUNCH LOG INFO */
  std::string l_emmc_path = m_pCfg->getEmmcOutputPath();
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  UI_32 abnrm_count = 0;

  l_dirp = opendir(l_emmc_path.c_str());
  if (l_dirp != NULL) {  // LCOV_EXCL_BR_LINE 5: opendir's error case.
    struct dirent l_dirent;
    struct dirent* next;
    while (0 == readdir_r(l_dirp, &l_dirent, &next) && next != NULL) {
      std::string l_findFileName = l_dirent.d_name;

//      if ((std::string::npos != l_findFileName.find("_ILG_RESET.tar.gz"))
//          || (std::string::npos != l_findFileName.find("_ERR.tar.gz"))) {
//        vect_abnrm.push_back(l_findFileName);
      if ((l_findFileName == ".") || (l_findFileName == "..")) {
        continue;
      } else if (std::string::npos != l_findFileName.find(SS_STORE_FILENAME_SYSILG_TERM)) {
        vect_abnrm.push_back(l_findFileName);
      } else if(std::string::npos != l_findFileName.find(SS_STORE_FILENAME__CWORD52__TERM)) {  // LCOV_EXCL_BR_LINE 8: SS_STORE_FILENAME__CWORD52__TERM can not be set  // NOLINT[whitespace/line_length]
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        vect_abnrm.push_back(l_findFileName);  // LCOV_EXCL_LINE 8: SS_STORE_FILENAME_SYSILG_TERM can not be set  // NOLINT[whitespace/line_length]
      } else if((std::string::npos != l_findFileName.find("_ILG_RESET.tar.gz")) ||
                (std::string::npos != l_findFileName.find("_ERR.tar.gz"))) {
        vect_abnrm.push_back(l_findFileName);
        abnrm_count++;

      } else if (std::string::npos
          != l_findFileName.find("_GRP_RELAUNCH.tar.gz")) {
        vect_rlnc.push_back(l_findFileName);
      } else if (std::string::npos != l_findFileName.find(".tar.gz")) {
        vect_nrm.push_back(l_findFileName);
      }
    }
    closedir(l_dirp);
  }

  switch (type) {
    case eErrorEventTypeProcessCrash:
    case eErrorEventTypeProcessExit:
    case eErrorEventTypeHeartBeatFailure:
    case eErrorEventTypeSystemLowMemory:
    case eErrorEventTypeBootMicroReset:
    case eErrorEventTypeModConnFailed:
    case eErrorEventTypeStartRespFailed:
    case eErrorEventTypeModuleInvokedResetRequest:

//      if ((UI_32) m_pCfg->m_emmcOutputErrMax <= vect_abnrm.size()) {
//        l_eStatus = deleteOldLog(l_emmc_path, vect_abnrm, f_archive_destination,
//                                 (UI_32) m_pCfg->m_emmcOutputErrMax);
      if ((UI_32)m_pCfg->m_emmcOutputErrMax <= abnrm_count) {
        l_eStatus = SS_LoggerStoreLogs_deleteOldLogAbnrm(l_emmc_path, vect_abnrm, f_archive_destination,
                                                         (UI_32)m_pCfg->m_emmcOutputErrMax, abnrm_count);

      }
      break;
    case eErrorEventTypeGroupRelaunch:
      if ((UI_32) m_pCfg->m_emmcOutputGrpRelaunchMax <= vect_rlnc.size()) {
        l_eStatus = deleteOldLog(l_emmc_path, vect_rlnc, f_archive_destination,
                                 (UI_32) m_pCfg->m_emmcOutputGrpRelaunchMax);
      }
      break;
    default:
      if ((UI_32) m_pCfg->m_emmcOutputMax <= vect_nrm.size()) {
        l_eStatus = deleteOldLog(l_emmc_path, vect_nrm, f_archive_destination,
                                 (UI_32) m_pCfg->m_emmcOutputMax);
      }
      break;
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

void CLoggerUtil::SetMilage(UI_32 f_Milage) {
  m_Milage = f_Milage;
}

std::string CLoggerUtil::GetUserInvokedFilename(HANDLE f_hApp, uint32_t f_time) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  std::string l_ret = "";
  std::stringstream l_nameStream;
  UI_32 l_seqNumber = 0;

  EFrameworkunifiedStatus l_eStatus = m_pCfg->GetIncrementAndPersistUserInvokedCounter(
      f_hApp, l_seqNumber);
  LOG_STATUS_IF_ERRORED(
      l_eStatus,
      "GetIncrementAndPersistUserInvokedCounter()");

  /* new counter value was not persisted if l_eStatus != eFrameworkunifiedStatusOK */

  l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << l_seqNumber << '_';
  l_nameStream << std::setw(8) << std::setfill('0') << std::right << std::dec << m_Milage << '_';
  l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << m_bootCount << '_';
  l_nameStream << std::setw(15) << GetTimeString(f_time) << ".tar.gz";

  l_ret = l_nameStream.str();

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_ret);
}

std::string CLoggerUtil::GetEmmcFilename(uint32_t f_time) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  std::string l_ret = "";
  std::stringstream l_nameStream;
  UI_32 l_counter = 0;

  m_pCfg->GetAndIncrementEmmcCounter(l_counter);
  l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << l_counter << '_';
  l_nameStream << std::setw(15) << this->GetTimeString(f_time) << ".tar.gz";

  l_ret = l_nameStream.str();

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_ret);
}

// LCOV_EXCL_START 8: dead code
std::string CLoggerUtil::GetEmmcErrorFilename(uint32_t f_time) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  std::string l_ret = "";
  std::stringstream l_nameStream;
  UI_32 l_counter = 0;

  m_pCfg->GetAndIncrementEmmcCounter(l_counter);
  l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << l_counter << '_';
  l_nameStream << std::setw(15) << this->GetTimeString(f_time) << "_ERR.tar.gz";

  l_ret = l_nameStream.str();

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_ret);
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 13:
std::string CLoggerUtil::GetEmmcNaviLogFilename(uint32_t f_time) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  std::string l_ret = "";
  std::stringstream l_nameStream;
  UI_32 l_counter = 1;

  l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << l_counter << '_';
  l_nameStream << std::setw(15) << this->GetTimeString(f_time) << ".log";

  l_ret = l_nameStream.str();

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_ret);
}
// LCOV_EXCL_STOP

std::string CLoggerUtil::GetTimeString(uint32_t f_time) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  std::string l_ret = "00000000_000000";
  struct tm local_time;
  Clock_getLocalTimeY2K38(&f_time, &local_time);
  char buffer[20];
  if (0
      != strftime(buffer, sizeof(buffer), "%Y%m%d_%H%M%S",  // LCOV_EXCL_BR_LINE 5: c code
                  &local_time)) {
    l_ret = buffer;
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_ret);
}

void CLoggerUtil::SetUsbMountPath(std::string usbpath) {
  strncpy(m_usbpath, usbpath.c_str(), USB_PATH_SIZE - 1);
  m_usbpath[USB_PATH_SIZE - 1] = '\0';
}
#ifdef RELEASE_BUILD
EFrameworkunifiedStatus CLoggerUtil::PathCheckAndCopyFile(std::string f_source,
    std::string f_destination) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
  std::string::size_type ret;

  ret = f_destination.find(m_usbpath);
  l_eStatus = CopyFile(f_source, f_destination);

  return l_eStatus;
}
#endif  // RELEASE_BUILD
/////////////////////////////////////////////////////////////////////////
///// Static Functions
/////////////////////////////////////////////////////////////////////////

EFrameworkunifiedStatus CLoggerUtil::CopyFile(std::string f_source,
                                 std::string f_destination) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  ssize_t l_nrd;
  ssize_t l_nwr;
  char l_buf[BUF_MAX_SIZE];

  int l_if = open(f_source.c_str(), O_RDONLY | O_CLOEXEC, 0);
  if (-1 == l_if) {  // LCOV_EXCL_BR_LINE 5: open's error case.
    // LCOV_EXCL_START 5: open's error case.
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "src open fail:%s,errno:%d,msg:%s",
           f_source.c_str(), errno, strerror(errno));
    l_eStatus = eFrameworkunifiedStatusFail;
    // LCOV_EXCL_STOP
  } else {
    bool isWebdav = false;
    std::string l_tmpDest;
    size_t l_pos = f_destination.find_last_of('/');

    l_tmpDest = f_destination;
    if (f_destination.find("/mnt/import") != std::string::npos) {  // LCOV_EXCL_BR_LINE 13:
      // if output to webdav's directory, don't create temporary file.
      // because of being unable to rename the file on it.
      isWebdav = true;
    }

    int l_of = open(l_tmpDest.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, 0640);
    if (-1 == l_of) {  // LCOV_EXCL_BR_LINE 5: open's error case.
      // LCOV_EXCL_START 5: open's error case.
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "dest open fail:%s,errno:%d,msg:%s",
             l_tmpDest.c_str(), errno, strerror(errno));
      l_eStatus = eFrameworkunifiedStatusFail;
      // LCOV_EXCL_STOP
    } else {
      do {
        l_nrd = read(l_if, l_buf, sizeof(l_buf));
        if (l_nrd == -1) {  // LCOV_EXCL_BR_LINE 5: read's error case.
          // LCOV_EXCL_START 5: read's error case.
          AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "read fail:%s,errno:%d,msg:%s",
                 f_source.c_str(), errno, strerror(errno));
          l_eStatus = eFrameworkunifiedStatusAccessError;
          break;
          // LCOV_EXCL_STOP
        }

        l_nwr = write(l_of, l_buf, l_nrd);
        if (l_nwr == -1) {  // LCOV_EXCL_BR_LINE 5: write's error case.
          // LCOV_EXCL_START 5: write's error case.
          AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
          if (ENOSPC == errno) {
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                   "write fail(ENOSPC):%s,errno:%d,msg:%s", l_tmpDest.c_str(),
                   errno, strerror(errno));
            l_eStatus = eFrameworkunifiedStatusErrNoEAGAIN;
          } else {
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "write fail:%s,errno:%d,msg%s",
                   l_tmpDest.c_str(), errno, strerror(errno));
            l_eStatus = eFrameworkunifiedStatusAccessError;
          }
          break;
          // LCOV_EXCL_STOP
        }
      } while (l_nrd > 0);

      fsync(l_of);
      close(l_of);

      if (l_eStatus != eFrameworkunifiedStatusOK) {  // LCOV_EXCL_BR_LINE 6: can not be fail
        // LCOV_EXCL_START 6: can not be fail
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        close(l_if);
        return l_eStatus;
        // LCOV_EXCL_STOP
      }

      {
        struct stat istat_buf, ostat_buf;
        int ret;
        ret = fstat(l_if, &istat_buf);
        if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: fstat's error case.
          // LCOV_EXCL_START 5: fstat's error case.
          AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. stat(%s). errno: %d, %s",
                 f_source.c_str(), errno, strerror(errno));
          // LCOV_EXCL_STOP
        } else {
          while (1) {
            ret = stat(l_tmpDest.c_str(), &ostat_buf);
            if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: stat's error case.
              // LCOV_EXCL_START 5: stat's error case.
              AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
              FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. stat(%s). errno: %d, %s",
                     l_tmpDest.c_str(), errno, strerror(errno));
              break;
              // LCOV_EXCL_STOP
            }
            if (ostat_buf.st_size >= istat_buf.st_size) {  // LCOV_EXCL_BR_LINE 5: read and write can not fail, so the size is same  // NOLINT[whitespace/line_length]
              break;
            }
            usleep(10000);  // interval
          }
        }
      }
    }
    close(l_if);

    if (!isWebdav) {  // LCOV_EXCL_BR_LINE 6: isWebdav is aways false
      if (rename(l_tmpDest.c_str(), f_destination.c_str()) != 0) {  // LCOV_EXCL_BR_LINE 5: rename's error case.
        // LCOV_EXCL_START 5: rename's error case.
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "rename %s to %s fail,errno:%d,msg:%s",
               l_tmpDest.c_str(), f_destination.c_str(), errno, strerror(errno));
        // LCOV_EXCL_STOP
      }
    }

    if (std::string::npos != l_pos) {  // LCOV_EXCL_BR_LINE 200: there is always '/' in f_destination
      std::string l_destPath = f_destination.substr(0, l_pos);
      SyncDir(l_destPath);
    } else {
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "dest invalid:%s", f_destination.c_str());  // LCOV_EXCL_LINE 200: there is always '/' in f_destination  // NOLINT[whitespace/line_length]
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}

// LCOV_EXCL_START 8: can not be called
EFrameworkunifiedStatus CLoggerUtil::CopyUntyped(std::string f_source,
                                    std::string f_destination) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  struct stat l_stat;
  int l_statRetVal;
  if (0 != access(f_source.c_str(), F_OK)) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           " Error. The specified file path and name does not exist: %s",
           f_source.c_str());
  } else if (0 != (l_statRetVal = lstat(f_source.c_str(), &l_stat))) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           " Error. stat() for file or directory returned error: %s -> %d",
           f_source.c_str(), l_statRetVal);
  } else if (0 != S_ISDIR(l_stat.st_mode)) {
    l_eStatus = CopyDirectory(f_source, f_destination);
  } else if (0 != S_ISLNK(l_stat.st_mode)) {
      // avoid duplicate copy for eErrorEventTypeInterfaceunifiedEmmcLogs: /nv/log/awlog -> /nv/BS/ss/logger_service/rwdata/log2/awlog
      if (f_source.find("/nv/BS/ss/logger_service/rwdata/awlog") == std::string::npos) {
        l_eStatus = CopyDirectory(f_source, f_destination);
      }
  } else if (0 != S_ISREG(l_stat.st_mode)) {
#ifdef RELEASE_BUILD
    l_eStatus = PathCheckAndCopyFile(f_source, f_destination);
#else
    l_eStatus = CopyFile(f_source, f_destination);
#endif  // RELEASE_BUILD
  } else {
    FRAMEWORKUNIFIEDLOG(
        ZONE_ERR,
        __FUNCTION__,
        " Error. specified file may be a symbolic link or other non 'regular' type. File: %s, mode: 0x%04X.",
        f_source.c_str(), l_stat.st_mode);
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 8: can not be called
EFrameworkunifiedStatus CLoggerUtil::CopyDirectory(std::string f_source,
                                      std::string f_destination) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  DIR * l_pDir = opendir(f_source.c_str());

  if (l_pDir == NULL) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Could not open directory: %s",
           f_source.c_str());
  } else {
    if (mkdir(f_destination.c_str(), 0775) == -1) {
      if (errno == EEXIST) {
        FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Directory: %s already exists.",
               f_destination.c_str());
      } else {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Could not create directory: %s. Errno: %s",
               f_destination.c_str(), strerror(errno));
        l_eStatus = eFrameworkunifiedStatusFail;
      }
    } else {
      SyncDir(f_destination);
      size_t l_pos = f_destination.find_last_of('/');
      if (l_pos != std::string::npos) {
        std::string l_destPath = f_destination.substr(0, l_pos);
        SyncDir(l_destPath);
      }
    }

    struct dirent l_pDirent;
    struct dirent* next;
    while (0 == readdir_r(l_pDir, &l_pDirent, &next) && next != NULL) {
      if ((0 != strcmp(l_pDirent.d_name, ".")) && /* Ignore special  . directory.                     */
      (0 != strcmp(l_pDirent.d_name, "..")) && /* Ignore special .. directory.                     */
      (0 != strcmp(l_pDirent.d_name, "lost+found")) && /* Ignore lost+found.                     */
      ('.' != l_pDirent.d_name[0])) {  /* Ignore hidden files                              */
        std::string l_fileSource = f_source;
        std::string l_fileDestination = f_destination;
        l_fileSource.append("/");
        l_fileSource.append(l_pDirent.d_name);
        l_fileDestination.append("/");
        l_fileDestination.append(l_pDirent.d_name);
        l_eStatus = CopyUntyped(l_fileSource, l_fileDestination);
      }
    }

    closedir(l_pDir);
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 8: can not be called
EFrameworkunifiedStatus CLoggerUtil::MoveFile(std::string f_source,
                                 std::string f_destination) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
#ifdef RELEASE_BUILD
  l_eStatus = CLoggerUtil::PathCheckAndCopyFile(f_source, f_destination);
  LOG_STATUS_IF_ERRORED(l_eStatus, "CLoggerUtil::PathCheckAndCopyFile()");
#else
  l_eStatus = CLoggerUtil::CopyFile(f_source, f_destination);
  LOG_STATUS_IF_ERRORED(l_eStatus, "CLoggerUtil::CopyFile()");
#endif  // RELEASE_BUILD
  if (eFrameworkunifiedStatusOK == l_eStatus) {
    l_eStatus = (0 == remove(f_source.c_str())) ? eFrameworkunifiedStatusOK : eFrameworkunifiedStatusFail;
    if (l_eStatus == eFrameworkunifiedStatusOK) {
      size_t l_pos = f_source.find_last_of('/');
      if (std::string::npos != l_pos) {
        std::string l_emmc_path = f_source.substr(0, l_pos);
        SyncDir(l_emmc_path);
      }
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 8: can not be called
EFrameworkunifiedStatus CLoggerUtil::MoveUntyped(std::string f_source,
                                    std::string f_destination) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
  struct stat l_stat;
  int l_statRetVal;
  if (0 != access(f_source.c_str(), F_OK)) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           " Error. The specified file path and name does not exist: %s",
           f_source.c_str());
  } else if (0 != (l_statRetVal = stat(f_source.c_str(), &l_stat))) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           " Error. stat() for file or directory returned error: %s -> %d",
           f_source.c_str(), l_statRetVal);
  } else if (0 != S_ISDIR(l_stat.st_mode)) {
    l_eStatus = CLoggerUtil::MoveDirectory(f_source, f_destination);
    LOG_STATUS_IF_ERRORED(
        l_eStatus, "CLoggerUtil::MoveDirectory(f_source, f_destination);");
  } else if (0 != S_ISREG(l_stat.st_mode)) {
    l_eStatus = CLoggerUtil::MoveFile(f_source, f_destination);
    LOG_STATUS_IF_ERRORED(l_eStatus,
                          "CLoggerUtil::MoveFile(f_source, f_destination)");
  } else {
    FRAMEWORKUNIFIEDLOG(
        ZONE_ERR,
        __FUNCTION__,
        " Error. specified file may be a symbolic link or other non 'regular' type. File: %s, mode: 0x%04X.",
        f_source.c_str(), l_stat.st_mode);
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 8: can not be called
EFrameworkunifiedStatus CLoggerUtil::MoveDirectory(std::string f_source,
                                      std::string f_destination) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  DIR * l_pDir = opendir(f_source.c_str());

  if (l_pDir == NULL) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Could not open directory: %s",
           f_source.c_str());
    l_eStatus = eFrameworkunifiedStatusFail;
  } else {
    errno = 0;
    if (mkdir(f_destination.c_str(), 0666) == -1) {
      if (errno == EEXIST) {
        FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Directory: %s already exists.",
               f_destination.c_str());
      } else {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Could not create directory: %s. Errno: %s",
               f_destination.c_str(), strerror(errno));
        l_eStatus = eFrameworkunifiedStatusFail;
      }
      errno = 0;
    } else {
      SyncDir(f_destination);
      size_t l_pos = f_destination.find_last_of('/');
      if (l_pos != std::string::npos) {
        std::string l_destPath = f_destination.substr(0, l_pos);
        SyncDir(l_destPath);
      }
    }

    if (eFrameworkunifiedStatusOK == l_eStatus) {  /* if directory already exists or has been newly created */
      struct dirent l_pDirent;
      struct dirent* next;

      while (0 == readdir_r(l_pDir, &l_pDirent, &next) && next != NULL) {
        if ((0 != strcmp(l_pDirent.d_name, ".")) && /* Ignore special  . directory.                     */
        (0 != strcmp(l_pDirent.d_name, "..")) && /* Ignore special .. directory.                     */
        (0 != strcmp(l_pDirent.d_name, "lost+found")) && /* Ignore lost+found.                     */
        ('.' != l_pDirent.d_name[0])) {  /* Ignore hidden files                              */
          std::string l_fileSource = f_source;
          std::string l_fileDestination = f_destination;
          l_fileSource.append("/");
          l_fileSource.append(l_pDirent.d_name);
          l_fileDestination.append("/");
          l_fileDestination.append(l_pDirent.d_name);
          l_eStatus = MoveUntyped(l_fileSource, l_fileDestination);
        }
      }
    } else {
    }
    closedir(l_pDir);
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return (l_eStatus);
}
// LCOV_EXCL_STOP

// LCOV_EXCL_START 200: can not be called
ELOGGERSERVICELOGTYPE CLoggerUtil::QueryLoggerservicelogType(std::string f_logname) {
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
//  if (f_logname.find("_ILG_RESET.tar.gz") != std::string::npos
  if (f_logname.find(SS_STORE_FILENAME_SYSILG_TERM) != std::string::npos) {
    return eLoggerservicelogTypeAbnormal;
  } else if (f_logname.find(SS_STORE_FILENAME__CWORD52__TERM) != std::string::npos) {
    return eLoggerservicelogTypeAbnormal;
  } else if (f_logname.find("_ILG_RESET.tar.gz") != std::string::npos

      || f_logname.find("_ERR.tar.gz") != std::string::npos) {
    return eLoggerservicelogTypeAbnormal;
  } else if (f_logname.find("_GRP_RELAUNCH.tar.gz") != std::string::npos) {
    return eLoggerservicelogTypeGrpRelaunch;
  } else if (f_logname.find(".tar.gz") != std::string::npos) {
    return eLoggerservicelogTypeNormal;
  } else {
    return eLoggerservicelogTypeOther;
  }
}
// LCOV_EXCL_STOP

void CLoggerUtil::SyncDir(std::string f_dir_path) {
  int fd = open(f_dir_path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
#define OPE_ERR_MSG "%s %s failed. errno:%d,msg:%s"
  if (fd == -1) {  // LCOV_EXCL_BR_LINE 5: open's error case.
    // LCOV_EXCL_START 5: open's error case.
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, OPE_ERR_MSG, f_dir_path.c_str(), "open",
           errno, strerror(errno));
    // LCOV_EXCL_STOP
  } else {
    if (fsync(fd) == -1) {  // LCOV_EXCL_BR_LINE 5: open's error case.
    // LCOV_EXCL_START 5: open's error case.
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, OPE_ERR_MSG, f_dir_path.c_str(), "fsync",
             errno, strerror(errno));
    // LCOV_EXCL_STOP
    }
    if (close(fd) == -1) {  // LCOV_EXCL_BR_LINE 5: open's error case.
    // LCOV_EXCL_START 5: open's error case.
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, OPE_ERR_MSG, f_dir_path.c_str(), "close",
             errno, strerror(errno));
    // LCOV_EXCL_STOP
    }
  }
#undef OPE_ERR_MSG
}