Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2023 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include "LogFileHandle.h"
21 :
22 : #include <fcntl.h>
23 : #include <unistd.h>
24 :
25 : namespace firebolt::rialto::logging
26 : {
27 8837 : LogFileHandle &LogFileHandle::instance()
28 : {
29 8837 : static LogFileHandle handle;
30 8837 : return handle;
31 : }
32 :
33 1 : void LogFileHandle::init(const std::string &path)
34 : {
35 1 : if (-1 == m_fd)
36 : {
37 1 : m_fd = open(path.c_str(), O_CLOEXEC | O_CREAT | O_WRONLY | O_TRUNC, 0664);
38 : }
39 : }
40 :
41 0 : int LogFileHandle::fd() const
42 : {
43 0 : return m_fd;
44 : }
45 :
46 0 : bool LogFileHandle::isOpen() const
47 : {
48 0 : return m_fd != -1;
49 : }
50 :
51 10 : LogFileHandle::LogFileHandle() : m_fd{-1} {}
52 :
53 10 : LogFileHandle::~LogFileHandle()
54 : {
55 10 : if (-1 != m_fd)
56 : {
57 1 : close(m_fd);
58 : }
59 9 : }
60 : } // namespace firebolt::rialto::logging
|