Use fmt instead of to_string and concating strings
[openal-soft.git] / al / error.cpp
blobf852d5ed4187949af730e90143b90ca199696d72
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2000 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include "error.h"
25 #ifdef _WIN32
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 #endif
30 #include <csignal>
31 #include <cstdarg>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <cstring>
35 #include <limits>
36 #include <optional>
37 #include <string>
38 #include <utility>
39 #include <vector>
41 #include "AL/al.h"
42 #include "AL/alc.h"
44 #include "al/debug.h"
45 #include "alc/alconfig.h"
46 #include "alc/context.h"
47 #include "core/logging.h"
48 #include "opthelpers.h"
49 #include "strutils.h"
52 namespace al {
53 context_error::context_error(ALenum code, const char *msg, ...) : mErrorCode{code}
55 /* NOLINTBEGIN(*-array-to-pointer-decay) */
56 std::va_list args;
57 va_start(args, msg);
58 setMessage(msg, args);
59 va_end(args);
60 /* NOLINTEND(*-array-to-pointer-decay) */
62 context_error::~context_error() = default;
63 } /* namespace al */
65 void ALCcontext::setError(ALenum errorCode, const char *msg, ...)
67 auto message = std::vector<char>(256);
69 /* NOLINTBEGIN(*-array-to-pointer-decay) */
70 std::va_list args, args2;
71 va_start(args, msg);
72 va_copy(args2, args);
73 int msglen{std::vsnprintf(message.data(), message.size(), msg, args)};
74 if(msglen >= 0 && static_cast<size_t>(msglen) >= message.size())
76 message.resize(static_cast<size_t>(msglen) + 1u);
77 msglen = std::vsnprintf(message.data(), message.size(), msg, args2);
79 va_end(args2);
80 va_end(args);
81 /* NOLINTEND(*-array-to-pointer-decay) */
83 if(msglen >= 0)
84 msg = message.data();
85 else
87 msg = "<internal error constructing message>";
88 msglen = static_cast<int>(strlen(msg));
91 WARN("Error generated on context %p, code 0x%04x, \"%s\"\n",
92 decltype(std::declval<void*>()){this}, errorCode, msg);
93 if(TrapALError)
95 #ifdef _WIN32
96 /* DebugBreak will cause an exception if there is no debugger */
97 if(IsDebuggerPresent())
98 DebugBreak();
99 #elif defined(SIGTRAP)
100 raise(SIGTRAP);
101 #endif
104 if(mLastThreadError.get() == AL_NO_ERROR)
105 mLastThreadError.set(errorCode);
107 debugMessage(DebugSource::API, DebugType::Error, static_cast<ALuint>(errorCode),
108 DebugSeverity::High, {msg, static_cast<uint>(msglen)});
111 /* Special-case alGetError since it (potentially) raises a debug signal and
112 * returns a non-default value for a null context.
114 AL_API auto AL_APIENTRY alGetError() noexcept -> ALenum
116 if(auto context = GetContextRef()) LIKELY
117 return alGetErrorDirect(context.get());
119 auto get_value = [](const char *envname, const char *optname) -> ALenum
121 auto optstr = al::getenv(envname);
122 if(!optstr)
123 optstr = ConfigValueStr({}, "game_compat", optname);
124 if(optstr)
126 char *end{};
127 auto value = std::strtoul(optstr->c_str(), &end, 0);
128 if(end && *end == '\0' && value <= std::numeric_limits<ALenum>::max())
129 return static_cast<ALenum>(value);
130 ERR("Invalid default error value: \"%s\"", optstr->c_str());
132 return AL_INVALID_OPERATION;
134 static const ALenum deferror{get_value("__ALSOFT_DEFAULT_ERROR", "default-error")};
136 WARN("Querying error state on null context (implicitly 0x%04x)\n", deferror);
137 if(TrapALError)
139 #ifdef _WIN32
140 if(IsDebuggerPresent())
141 DebugBreak();
142 #elif defined(SIGTRAP)
143 raise(SIGTRAP);
144 #endif
146 return deferror;
149 FORCE_ALIGN ALenum AL_APIENTRY alGetErrorDirect(ALCcontext *context) noexcept
151 ALenum ret{context->mLastThreadError.get()};
152 if(ret != AL_NO_ERROR) UNLIKELY
153 context->mLastThreadError.set(AL_NO_ERROR);
154 return ret;