OpenVPN 3 Core Library
Loading...
Searching...
No Matches
logutil.hpp
Go to the documentation of this file.
1
// OpenVPN -- An application to securely tunnel IP networks
2
// over a single port, with support for SSL/TLS-based
3
// session authentication and key exchange,
4
// packet encryption, packet authentication, and
5
// packet compression.
6
//
7
// Copyright (C) 2012- OpenVPN Inc.
8
//
9
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10
//
11
12
#pragma once
13
14
#include <windows.h>
15
16
#include <string>
17
18
#include <
openvpn/common/exception.hpp
>
19
#include <
openvpn/common/wstring.hpp
>
20
#include <
openvpn/time/timestr.hpp
>
21
#include <
openvpn/win/winerr.hpp
>
22
#include <
openvpn/win/secattr.hpp
>
23
#include <
openvpn/win/scoped_handle.hpp
>
24
25
namespace
openvpn::Win::LogUtil
{
26
27
inline
void
log
(
const
HANDLE file,
const
std::string &str)
28
{
29
DWORD n_written;
30
const
std::string line =
date_time
() +
' '
+ str;
31
::WriteFile(file, line.c_str(),
static_cast<
DWORD
>
(line.length()), &n_written, NULL);
32
}
33
34
inline
ScopedHANDLE
create_file
(
const
std::string &fn,
35
const
std::string &sddl_string,
36
bool
append)
37
{
38
SecurityAttributes
sa(sddl_string,
true
, fn);
// fn passed as title only
39
const
std::wstring wfn = wstring::from_utf8(fn);
40
ScopedHANDLE
file(::CreateFileW(
41
wfn.c_str(),
42
GENERIC_WRITE,
43
FILE_SHARE_READ,
44
&sa.
sa
,
45
append ? OPEN_ALWAYS : CREATE_ALWAYS,
46
FILE_ATTRIBUTE_NORMAL,
47
NULL));
48
if
(!file.
defined
())
49
{
50
const
Win::LastError
err;
51
OPENVPN_THROW_EXCEPTION
(
"Win::LogFile: failed to open "
<< fn <<
" : "
<< err.message());
52
}
53
54
// append to logfile?
55
if
(append)
56
{
57
if
(::SetFilePointer(file(), 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
58
{
59
const
Win::LastError
err;
60
OPENVPN_THROW_EXCEPTION
(
"Win::LogFile: cannot append to "
<< fn <<
" : "
<< err.message());
61
}
62
}
63
return
file;
64
}
65
66
}
// namespace openvpn::Win::LogUtil
openvpn::Win::ScopedHANDLE
Definition
scoped_handle.hpp:24
openvpn::Win::ScopedHANDLE::defined
bool defined() const
Definition
scoped_handle.hpp:48
exception.hpp
OPENVPN_THROW_EXCEPTION
#define OPENVPN_THROW_EXCEPTION(stuff)
Definition
exception.hpp:166
openvpn::Win::LogUtil
Definition
logutil.hpp:25
openvpn::Win::LogUtil::log
void log(const HANDLE file, const std::string &str)
Definition
logutil.hpp:27
openvpn::Win::LogUtil::create_file
ScopedHANDLE create_file(const std::string &fn, const std::string &sddl_string, bool append)
Definition
logutil.hpp:34
openvpn::date_time
std::string date_time()
Definition
timestr.hpp:139
scoped_handle.hpp
secattr.hpp
openvpn::Win::LastError
Definition
winerr.hpp:31
openvpn::Win::SecurityAttributes
Definition
secattr.hpp:25
openvpn::Win::SecurityAttributes::sa
SECURITY_ATTRIBUTES sa
Definition
secattr.hpp:54
timestr.hpp
winerr.hpp
wstring.hpp
openvpn
win
logutil.hpp
Generated by
1.9.8