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
24
26
27inline 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
34inline 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
#define OPENVPN_THROW_EXCEPTION(stuff)
void log(const HANDLE file, const std::string &str)
Definition logutil.hpp:27
ScopedHANDLE create_file(const std::string &fn, const std::string &sddl_string, bool append)
Definition logutil.hpp:34
std::string date_time()
Definition timestr.hpp:139