1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SyncML_Log.H"
18
19/*==================================================================================================
20
21Function:    SyncML_Log::SyncML_Log
22
23Description: Constructor for the Log object
24
25==================================================================================================*/
26SyncML_Log::SyncML_Log()
27{
28    this->fileHandle = NULL;
29}
30
31/*==================================================================================================
32
33Function:    SyncML_DM_Log::InitLog
34
35Description: Open log
36
37==================================================================================================*/
38SYNCML_DM_RET_STATUS_T SyncML_Log::InitLog(CPCHAR logFileName)
39{
40    if (fileHandle != NULL)
41        CloseLog();
42
43    INT32 modeFlag = XPL_FS_FILE_RDWR;
44    // If file does not exist use write mode instead of read/write to prevent file I/O error
45    if (!XPL_FS_Exist(logFileName))
46        modeFlag = XPL_FS_FILE_WRITE;
47
48    fileHandle = new DMFileHandler(logFileName);
49    if(fileHandle == NULL)
50       return SYNCML_DM_IO_FAILURE;
51    if (fileHandle->open(modeFlag) != SYNCML_DM_SUCCESS)
52    {
53       fileHandle->deleteFile();
54       delete fileHandle;
55       fileHandle = NULL;
56       return SYNCML_DM_IO_FAILURE;
57    }
58
59    return setLogFileHandle(fileHandle);
60}
61
62/*==================================================================================================
63
64Function:    UnInitLog
65
66Description: Uninitialize log
67
68==================================================================================================*/
69SYNCML_DM_RET_STATUS_T SyncML_Log::UnInitLog()
70{
71    return SYNCML_DM_SUCCESS;
72}
73
74/*==================================================================================================
75
76Function:   :~SyncML_Log
77
78Description: Destructor for the Log object
79
80==================================================================================================*/
81SyncML_Log::~SyncML_Log()
82{
83    /* The fileHandle reference is deleted by the calling class */
84}
85
86/*==================================================================================================
87
88Function:    setLogFileHandle
89
90Description:  Accessor method for the file reader
91
92==================================================================================================*/
93SYNCML_DM_RET_STATUS_T
94SyncML_Log::setLogFileHandle(DMFileHandler* fileHandle)
95{
96    this->fileHandle = fileHandle;
97    return SYNCML_DM_SUCCESS;
98}
99
100/*==================================================================================================
101
102Function:    getLogFileHandle
103
104Description:  Accessor method for the file reader
105
106==================================================================================================*/
107DMFileHandler*
108SyncML_Log::getLogFileHandle()
109{
110    return this->fileHandle;
111}
112
113/*==================================================================================================
114
115Function:    RemoveLog
116
117Description:  Remove  log file
118
119==================================================================================================*/
120SYNCML_DM_RET_STATUS_T SyncML_Log::RemoveLog()
121{
122    UnInitLog();
123    if(fileHandle != NULL)
124    {
125        fileHandle->deleteFile();
126        delete fileHandle;
127        fileHandle = NULL;
128    }
129    return SYNCML_DM_SUCCESS;
130}
131
132/*==================================================================================================
133
134Function:    CloseLog
135
136Description:  Close log file
137
138==================================================================================================*/
139SYNCML_DM_RET_STATUS_T SyncML_Log::CloseLog()
140{
141    UnInitLog();
142    if (fileHandle != NULL)
143    {
144        fileHandle->close();
145        delete fileHandle;
146        fileHandle = NULL;
147    }
148    return SYNCML_DM_SUCCESS;
149}
150
151/*==================================================================================================
152
153Function:    SyncML_Log::playLog
154
155Description:  Log file playback
156
157==================================================================================================*/
158SYNCML_DM_RET_STATUS_T SyncML_Log::playLog()
159{
160    return SYNCML_DM_SUCCESS;
161}
162
163/*==================================================================================================
164
165Function:    SyncML_Log::playLog
166
167Description:  Log file playback and remove
168
169==================================================================================================*/
170SYNCML_DM_RET_STATUS_T SyncML_Log::playLog(CPCHAR logFileName)
171{
172    SYNCML_DM_RET_STATUS_T retStatus = SYNCML_DM_SUCCESS;
173    retStatus = InitLog(logFileName);
174    if (retStatus == SYNCML_DM_SUCCESS)
175    {
176        retStatus = playLog();
177    }
178    RemoveLog();
179    return retStatus;
180}
181