summaryrefslogtreecommitdiffhomepage
path: root/ovr_sdk_win_23.0.0/LibOVRKernel/Src/Kernel/OVR_Error.h
blob: a9d91f1be0b4d789b042abae56432545c8e2d4e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/************************************************************************************

Filename    :   OVR_Error.h
Content     :   Structs and functions for handling OVRErrors
Created     :   February 15, 2015
Copyright   :   Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.

Licensed under the Oculus Master SDK License Version 1.0 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

https://developer.oculus.com/licenses/oculusmastersdk-1.0

Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#pragma once

#include <Logging/Logging-fwd.h>
#include <chrono>
#include <functional>
#include <utility>
#include "Kernel/OVR_Array.h"
#include "Kernel/OVR_Hash.h"
#include "Kernel/OVR_String.h"
#include "Kernel/OVR_System.h"
#include "Kernel/OVR_Threads.h"

OVR_DISABLE_MSVC_WARNING(4351) // new behavior: elements of array will be default initialized

#ifndef OVR_RESULT_DEFINED
#define OVR_RESULT_DEFINED ///< Allows ovrResult to be independently defined.
/// API call results are represented at the highest level by a single ovrResult.
typedef int32_t ovrResult;
#endif

/// \brief Indicates if an ovrResult indicates success.
///
/// Some functions return additional successful values other than ovrSucces and
/// require usage of this macro to indicate successs.
///
#if !defined(OVR_SUCCESS)
#define OVR_SUCCESS(result) (result >= 0)
#endif

/// \brief Indicates if an ovrResult indicates an unqualified success.
///
/// This is useful for indicating that the code intentionally wants to
/// check for result == ovrSuccess as opposed to OVR_SUCCESS(), which
/// checks for result >= ovrSuccess.
///
#if !defined(OVR_UNQUALIFIED_SUCCESS)
#define OVR_UNQUALIFIED_SUCCESS(result) (result == ovrSuccess)
#endif

/// \brief Indicates if an ovrResult indicates failure.
///
#if !defined(OVR_FAILURE)
#define OVR_FAILURE(result) (!OVR_SUCCESS(result))
#endif

// Success is a value greater or equal to 0, while all error types are negative values.
#ifndef OVR_SUCCESS_DEFINED
#define OVR_SUCCESS_DEFINED ///< Allows ovrResult to be independently defined.
typedef enum ovrSuccessType_ {
  /// This is a general success result. Use OVR_SUCCESS to test for success.
  ovrSuccess = 0,
} ovrSuccessType;
#endif

/// -----------------------------------------------------------------------------
/// ***** OVR_FILE / OVR_LINE
///
#if !defined(OVR_FILE)
#if defined(OVR_BUILD_DEBUG)
#define OVR_FILE __FILE__
#define OVR_LINE __LINE__
#else
#define OVR_FILE nullptr
#define OVR_LINE 0
#endif
#endif

// LOG_VAARG_ATTRIBUTE macro, enforces printf-style formatting for message types
#ifdef __GNUC__
#define OVR_LOG_VAARG_ATTRIBUTE(a, b) __attribute__((format(printf, a, b)))
#else
#define OVR_LOG_VAARG_ATTRIBUTE(a, b)
#endif

namespace OVR {

/// -----------------------------------------------------------------------------
/// ***** OVR_MAKE_ERROR, OVR_MAKE_ERROR_F, OVR_MAKE_SYS_ERROR, OVR_MAKE_SYS_ERROR_F
///
/// Declaration:
///   OVRError OVR_MAKE_ERROR(ovrResult r, const char* description);
///   OVRError OVR_MAKE_ERROR_F(ovrResult r, const char* format, ...);
///
///   OVRError OVR_MAKE_SYS_ERROR(ovrResult r, ovrSysErrorCode sysCode, const char* description);
///   OVRError OVR_MAKE_SYS_ERROR_F(ovrResult r, ovrSysErrorCode sysCode, const char* format, ...);
///
/// Example usage:
///      OVRError InitGraphics()
///      {
///          if(!GraphicsCardPresent())
///          {
///              return OVR_MAKE_ERROR(ovrError_GraphicsInit, "Failed to init graphics; graphics
///                                                             support absent.");
///          }
///
///          HRESULT hr = pDevice->CreateTexture2D(&dsDesc, nullptr, &Texture);
///          if(FAILED(hr))
///          {
///              return OVR_MAKE_SYS_ERROR_F(ovrError_GraphicsInit, hr, "Failed to create texture of
///                                                size %u x %u", dsDesc.Width, dsDesc.Height);
///          }
///          or:
///              OVR_HR_CHECK_RET_ERROR(ovrError_GraphicsInit, hr, "Failed to create texture of size
///                                                %u x %u", dsDesc.Width, dsDesc.Height);
///
///          return ovrSuccess; // Converts to an OVRError instance that has no error.
///      }
///
#define OVR_MAKE_ERROR(errorCode, pDescription) \
  OVR::MakeError(                               \
      (errorCode),                              \
      OVR::ovrSysErrorCodeType::None,           \
      OVR::ovrSysErrorCodeSuccess,              \
      nullptr,                                  \
      OVR_FILE,                                 \
      OVR_LINE,                                 \
      true,                                     \
      true,                                     \
      nullptr,                                  \
      "%s",                                     \
      (pDescription))

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_MAKE_ERROR_F(errorCode, ...) \
  OVR::MakeError(                        \
      (errorCode),                       \
      OVR::ovrSysErrorCodeType::None,    \
      OVR::ovrSysErrorCodeSuccess,       \
      nullptr,                           \
      OVR_FILE,                          \
      OVR_LINE,                          \
      true,                              \
      true,                              \
      nullptr,                           \
      __VA_ARGS__)

#define OVR_MAKE_SYS_ERROR(errorCode, sysErrorCode, pDescription) \
  OVR::MakeError(                                                 \
      (errorCode),                                                \
      OVR::ovrSysErrorCodeType::OS,                               \
      (sysErrorCode),                                             \
      nullptr,                                                    \
      OVR_FILE,                                                   \
      OVR_LINE,                                                   \
      true,                                                       \
      true,                                                       \
      nullptr,                                                    \
      "%s",                                                       \
      (pDescription))

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_MAKE_SYS_ERROR_F(errorCode, sysErrorCode, ...) \
  OVR::MakeError(                                          \
      (errorCode),                                         \
      OVR::ovrSysErrorCodeType::OS,                        \
      (sysErrorCode),                                      \
      nullptr,                                             \
      OVR_FILE,                                            \
      OVR_LINE,                                            \
      true,                                                \
      true,                                                \
      nullptr,                                             \
      __VA_ARGS__)

#define OVR_MAKE_VULKAN_ERROR(errorCode, sysErrorCode, pDescription) \
  OVR::MakeError(                                                    \
      (errorCode),                                                   \
      OVR::ovrSysErrorCodeType::Vulkan,                              \
      (sysErrorCode),                                                \
      nullptr,                                                       \
      OVR_FILE,                                                      \
      OVR_LINE,                                                      \
      true,                                                          \
      true,                                                          \
      nullptr,                                                       \
      "%s",                                                          \
      (pDescription))

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_MAKE_VULKAN_ERROR_F(errorCode, sysErrorCode, ...) \
  OVR::MakeError(                                             \
      (errorCode),                                            \
      OVR::ovrSysErrorCodeType::Vulkan,                       \
      (sysErrorCode),                                         \
      nullptr,                                                \
      OVR_FILE,                                               \
      OVR_LINE,                                               \
      true,                                                   \
      true,                                                   \
      nullptr,                                                \
      __VA_ARGS__)

#define OVR_MAKE_OPENGL_ERROR(errorCode, sysErrorCode, pDescription) \
  OVR::MakeError(                                                    \
      (errorCode),                                                   \
      OVR::ovrSysErrorCodeType::OpenGL,                              \
      (sysErrorCode),                                                \
      nullptr,                                                       \
      OVR_FILE,                                                      \
      OVR_LINE,                                                      \
      true,                                                          \
      true,                                                          \
      nullptr,                                                       \
      "%s",                                                          \
      (pDescription))

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_MAKE_OPENGL_ERROR_F(errorCode, sysErrorCode, ...) \
  OVR::MakeError(                                             \
      (errorCode),                                            \
      OVR::ovrSysErrorCodeType::OpenGL,                       \
      (sysErrorCode),                                         \
      nullptr,                                                \
      OVR_FILE,                                               \
      OVR_LINE,                                               \
      true,                                                   \
      true,                                                   \
      nullptr,                                                \
      __VA_ARGS__)

// Consider using OVR_MAKE_QUIET_ERROR() instead of OVR_MAKE_ERROR() where the error
// should not automatically log/assert.  If the error is normal (HMD unplugged) or
// repetitive then please use OVR_MAKE_QUIET_ERROR().

#define OVR_MAKE_QUIET_ERROR(errorCode, pDescription) \
  OVR::MakeError(                                     \
      (errorCode),                                    \
      OVR::ovrSysErrorCodeType::None,                 \
      OVR::ovrSysErrorCodeSuccess,                    \
      nullptr,                                        \
      OVR_FILE,                                       \
      OVR_LINE,                                       \
      false,                                          \
      false,                                          \
      nullptr,                                        \
      "%s",                                           \
      (pDescription))

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_MAKE_QUIET_ERROR_F(errorCode, ...) \
  OVR::MakeError(                              \
      (errorCode),                             \
      OVR::ovrSysErrorCodeType::None,          \
      OVR::ovrSysErrorCodeSuccess,             \
      nullptr,                                 \
      OVR_FILE,                                \
      OVR_LINE,                                \
      false,                                   \
      false,                                   \
      nullptr,                                 \
      __VA_ARGS__)

#define OVR_MAKE_QUIET_SYS_ERROR(errorCode, sysErrorCode, pDescription) \
  OVR::MakeError(                                                       \
      (errorCode),                                                      \
      OVR::ovrSysErrorCodeType::OS,                                     \
      (sysErrorCode),                                                   \
      nullptr,                                                          \
      OVR_FILE,                                                         \
      OVR_LINE,                                                         \
      false,                                                            \
      false,                                                            \
      nullptr,                                                          \
      "%s",                                                             \
      (pDescription))

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_MAKE_QUIET_SYS_ERROR_F(errorCode, sysErrorCode, ...) \
  OVR::MakeError(                                                \
      (errorCode),                                               \
      ovrSysErrorCodeType::OS,                                   \
      (sysErrorCode),                                            \
      nullptr,                                                   \
      OVR_FILE,                                                  \
      OVR_LINE,                                                  \
      false,                                                     \
      false,                                                     \
      nullptr,                                                   \
      __VA_ARGS__)

// Consider using OVR_MAKE_NOASSERT_ERROR() instead of OVR_MAKE_ERROR() where the error
// should not automatically log/assert.  If the error is normal (HMD unplugged) or
// repetitive then please use OVR_MAKE_NOASSERT_ERROR().

#define OVR_MAKE_NOASSERT_ERROR(errorCode, pDescription) \
  OVR::MakeError(                                        \
      (errorCode),                                       \
      OVR::ovrSysErrorCodeType::None,                    \
      OVR::ovrSysErrorCodeSuccess,                       \
      nullptr,                                           \
      OVR_FILE,                                          \
      OVR_LINE,                                          \
      true,                                              \
      false,                                             \
      nullptr,                                           \
      "%s",                                              \
      (pDescription))

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_MAKE_NOASSERT_ERROR_F(errorCode, ...) \
  OVR::MakeError(                                 \
      (errorCode),                                \
      OVR::ovrSysErrorCodeType::None,             \
      OVR::ovrSysErrorCodeSuccess,                \
      nullptr,                                    \
      OVR_FILE,                                   \
      OVR_LINE,                                   \
      true,                                       \
      false,                                      \
      nullptr,                                    \
      __VA_ARGS__)

#define OVR_MAKE_NOASSERT_SYS_ERROR(errorCode, sysErrorCode, pDescription) \
  OVR::MakeError(                                                          \
      (errorCode),                                                         \
      OVR::ovrSysErrorCodeType::OS,                                        \
      (sysErrorCode),                                                      \
      nullptr,                                                             \
      OVR_FILE,                                                            \
      OVR_LINE,                                                            \
      true,                                                                \
      false,                                                               \
      nullptr,                                                             \
      "%s",                                                                \
      (pDescription))

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_MAKE_NOASSERT_SYS_ERROR_F(errorCode, sysErrorCode, ...) \
  OVR::MakeError(                                                   \
      (errorCode),                                                  \
      OVR::ovrSysErrorCodeType::OS,                                 \
      (sysErrorCode),                                               \
      nullptr,                                                      \
      OVR_FILE,                                                     \
      OVR_LINE,                                                     \
      true,                                                         \
      false,                                                        \
      nullptr,                                                      \
      __VA_ARGS__)

// Set the TLS last error:
#define OVR_SET_ERROR(ovrError) OVR::SetError(ovrError)

// Log the error if it has not already been logged:
#define OVR_LOG_ERROR(ovrError) OVR::LogError(ovrError)

// Check an HRESULT error code and assert/log on failure:
#define OVR_HR_CHECK_RET_ERROR(errorCode, sysErrorCode, pDescription)               \
  if (FAILED(sysErrorCode)) {                                                       \
    return OVR_MAKE_SYS_ERROR_F((errorCode), (sysErrorCode), "%s", (pDescription)); \
  }

// Note: The format string is the first part of the .../__VA_ARGS__ as per the C99-C++11 Standards.
#define OVR_HR_CHECK_RET_ERROR_F(errorCode, sysErrorCode, ...)             \
  if (FAILED(sysErrorCode)) {                                              \
    return OVR_MAKE_SYS_ERROR_F((errorCode), (sysErrorCode), __VA_ARGS__); \
  }

/// -----------------------------------------------------------------------------
/// ***** ovrSysErrorCodeType
///
enum class ovrSysErrorCodeType {
  None,
  OS, /// Windows: HRESULT or DWORD system error code from GetLastError.
  ALVR, /// AMD LiquidVR error
  NVAPI, /// NVidia API error
  NVENC, /// NVidia video encode API error
  Vulkan, /// Vulkan graphics API
  OpenGL /// OpenGL graphics API
};

/// -----------------------------------------------------------------------------
/// ***** ovrSysErrorCode
///
/// Identifies a platform- or API-specific error identifier.
/// The error space is identified by ovrSysErrorCodeType.
///
typedef uint32_t ovrSysErrorCode;

/// -----------------------------------------------------------------------------
/// ***** ovrSysErrorCodeSuccess
///
/// Identifies a ovrSysErrorCode that's success.
///
const ovrSysErrorCode ovrSysErrorCodeSuccess = 0;

/// -----------------------------------------------------------------------------
/// ***** ovrSysErrorCodeNone
///
/// Identifies a ovrSysErrorCode that's un-set.
///
const ovrSysErrorCode ovrSysErrorCodeNone = 0;

// SysClockTime is a C++11 equivalent to C time_t.
typedef std::chrono::time_point<std::chrono::system_clock> SysClockTime;

/// -----------------------------------------------------------------------------
/// ***** OVRError
///
/// Represents an error and relevant information about it.
/// While you can create error instances directly via this class, it's better if
/// you create them via the OVR_MAKE_ERROR family of macros, or at least via the
/// MakeError function.
///
/// Relevant design analogues:
///     https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSError_Class/
///     https://msdn.microsoft.com/en-us/library/windows/desktop/ms723041%28v=vs.85%29.aspx
///
class OVRError {
 private:
  // Cannot convert boolean to OVRError - It must be done explicitly.
  explicit OVRError(bool) = delete;

 public:
  OVRError();
  OVRError(ovrResult code); // Intentionally not explicit.
  OVRError(ovrResult code, const char* pFormat, ...);

  OVRError(const OVRError& OVRError);
  OVRError(OVRError&& OVRError);

  virtual ~OVRError();

  // Construct a success code.  Use Succeeded() to check for success.
  static OVRError Success() {
    return OVRError();
  }

  OVRError& operator=(const OVRError& OVRError);
  OVRError& operator=(OVRError&& OVRError);

  // Use this to check if result is a success code
  bool Succeeded() const {
    return Code >= ovrSuccess;
  }
  bool Failed() const {
    return !Succeeded();
  }

  // Sets the OVRTime, ClockTime, Backtrace to current values.
  void SetCurrentValues(); // To do: Come up with a more appropiate name.

  // Clears all members to a newly default-constructed state.
  void Reset();

  // Get the full error string for this error. May include newlines.
  String GetErrorString() const;

  // Property accessors
  void SetCode(ovrResult code);
  ovrResult GetCode() const;

  // Example usage:
  //   ovrResult SomeFunction() {
  //     OVRError err = SomeOtherFunction();
  //     return err;
  //   }
  operator ovrResult() const {
    return Code;
  }

  void SetSysCode(
      ovrSysErrorCodeType sysCodeType,
      ovrSysErrorCode sysCode,
      const char* sysCodeString = nullptr);
  ovrSysErrorCodeType GetSysCodeType() const;
  ovrSysErrorCode GetSysCode() const;

  void SetDescription(const char* pDescription);
  String GetDescription() const;

  void SetContext(const char* pContext);
  String GetContext() const;

  void SetOVRTime(double ovrTime);
  double GetOVRTime() const;

  void SetSysClockTime(const SysClockTime& clockTime);
  SysClockTime GetSysClockTime() const;

  static const int64_t kLogLineUnset = -1;
  void SetLogLine(int64_t logLine);
  int64_t GetLogLine() const;

  bool IsAlreadyLogged() const {
    return AlreadyLogged;
  }
  void SetAlreadyLogged() {
    AlreadyLogged = true;
  }
  void ResetAlreadyLogged() {
    AlreadyLogged = false;
  }

  void SetSource(const char* pSourceFilePath, int sourceFileLine);
  std::pair<String, int> GetSource() const;

  typedef OVR::Array<void*> AddressArray;
  AddressArray GetBacktrace() const;

  static void RegisterResultCodeName(ovrResult number, const char* name);

 protected:
  mutable StringBuffer ErrorString; /// Complete error string describing the variables below.
  ovrResult Code; /// The main ovrResult, which is a high level error id.
  ovrSysErrorCodeType SysCodeType;
  ovrSysErrorCode SysCode; /// ovrSysErrorCodeSuccess means no system error code.
  char SysCodeStr[64]; /// String describing just the sys code error.
  String Description; /// Unlocalized error description string.
  String Context; /// Context string. For example, for a file open failure this is the file path.
  double OVRTime; /// Time when the error was generated. Same format as OVR time.
  SysClockTime ClockTime; /// Wall clock time.
  int64_t LogLine; /// Log line of the error. -1 if not set (not logged).
  String SourceFilePath; /// The __FILE__ where the error was first encountered.
  int SourceFileLine; /// The __LINE__ where the error was first encountered.
  AddressArray Backtrace; /// Backtrace at point of error. May be empty in publicly released builds.
  bool AlreadyLogged; /// Error has already been logged to avoid double-printing it.
};

/// -----------------------------------------------------------------------------
/// ***** SetError
///
/// This function sets the error as the last error via the TLS last error class.
///
void SetError(OVRError& ovrError);

/// -----------------------------------------------------------------------------
/// ***** LogError
///
/// Utility function for logging an error based on the Log subsystem.
///
void LogError(OVRError& ovrError);

/// -----------------------------------------------------------------------------
/// ***** MakeError
///
/// Utility function for making an error, logging it, and setting it as the last error for the
/// current thread. It's preferred to instead use the OVR_MAKE_ERROR macro functions, as they handle
/// file/line functionality cleanly between debug and release. The "quiet" parameter will prevent it
/// from automatically logging/asserting.
///
OVRError MakeError(
    ovrResult errorCode,
    ovrSysErrorCodeType sysCodeType,
    ovrSysErrorCode sysCode,
    const char* sysCodeString, // Optional pre-generated sys error code string.
    const char* pSourceFile,
    int sourceLine,
    bool logError,
    bool assertError,
    const char* pContext,
    const char* pDescriptionFormat,
    ...) OVR_LOG_VAARG_ATTRIBUTE(10, 11);

// -----------------------------------------------------------------------------
// ***** LastErrorTLS
//
// We don't use C++11 thread-local storage nor C-style __thread/__declsped(thread)
// to manager thread-local storage, as neither of these provide a means for us
// to control the lifetime of the data. Rather it can be controlled only
// passively by the thread's lifetime. Similarly we don't use pthread_getspecific
// pthread_setspecific (and Windows equivalents) because they too don't let us
// control the lifetime of the data. Our solution is to have a map of threads
// to thread-specific data, and we can clear the entire map on ovrShutdown as-needed.
// this scheme is not as fast as the aforementioned schemes but it doesn't need to
// be fast for our use.
//
// We use pointers below instead of concrete objects because we want to have their
// lifetimes entirely controlled by ovr_Initialize/ovr_Shutdown.

class LastErrorTLS : public NewOverrideBase, public SystemSingletonBase<LastErrorTLS> {
  OVR_DECLARE_SINGLETON(LastErrorTLS);

 public:
  OVRError& LastError();

 protected:
  // Protect hash from multiple thread access.
  Lock TheLock;

  // Map thread-id to OVRError objects.
  typedef Hash<ThreadId, OVRError> TLSHash;
  TLSHash TLSDictionary;
};

/// -----------------------------------------------------------------------------
/// ***** GetErrorCodeName
///
/// Utility function which converts an ovrResult error code to a string which matches
/// errorCode's ovrResult enumeration identifier.
///
const char* GetErrorCodeName(ovrResult errorCode);

/// -----------------------------------------------------------------------------
/// ***** GetErrorCodeString
///
/// Utility function which converts an ovrResult error code to a readable string version.
///
bool GetErrorCodeString(ovrResult errorCode, bool prefixErrorCode, OVR::String& sResult);

/// -----------------------------------------------------------------------------
/// ***** GetSysErrorCodeString
///
/// Utility function which converts a system error to a string. Similar to the Windows FormatMessage
/// function and the Unix strerror_r function.
/// If prefixErrorCode is true then the string is prefixed with "<code>: "
/// Returns true if the sysErrorCode was a known valid code and a string was produced from it.
/// Else the returned string will be empty. The returned string may have tabs or newlines.
/// Users of OVR_MAKE_SYS_ERROR and MakeError don't need to call this function, as it's done
/// automatically internally.
///
bool GetSysErrorCodeString(
    ovrSysErrorCodeType sysErrorCodeType,
    ovrSysErrorCode sysErrorCode,
    bool prefixErrorCode,
    OVR::String& sResult);

//-------------------------------------------------------------------------------------
// C++ Exception state

// CPPExceptionInfo contains information about a possible exception state for the current library.
// We cannot safely use the OVRError system for this, as it could fail when we are in an exceptional
// state. It would be a non-trivial effort to make the OVRError system work in a way that doesn't
// allocate memory, for various reasons.

struct CPPExceptionInfo {
  CPPExceptionInfo() OVR_NOEXCEPT : ExceptionOccurred(false), Description() {}

  void Reset() OVR_NOEXCEPT {
    ExceptionOccurred = false;
    Description[0] = '\0';
  }

  void FromStdException(const std::exception& e) OVR_NOEXCEPT {
    ExceptionOccurred = true;
    OVR_strlcpy(Description, e.what(), sizeof(Description));
  }

  void FromString(const char* str) OVR_NOEXCEPT {
    ExceptionOccurred = true;
    OVR_strlcpy(Description, str, sizeof(Description));
  }

  // If this get set to true then it should never be set back to false
  // except by an unloading of the module (i.e. DLL) itself.
  bool ExceptionOccurred;

  // Describes the exception in a way that makes no external calls
  // nor allocates any memory.
  char Description[256];
};

extern OVR::CPPExceptionInfo gCPPExceptionInfo;

// Helper macros to make repetitive exception handling tasks non-redundant.
//
// Example usage:
//    OVR_PUBLIC_FUNCTION(void) ovr_DoSomething(ovrSession session)
//    {
//        OVR_CAPI_TRY_VOID
//        {
//            HMDState* hmds = GetHMDStateFromOvrHmd(session);
//            if (hmds)
//                hmds->DoSomething();
//        }
//        OVR_CAPI_CATCH
//    }
//
//    OVR_PUBLIC_FUNCTION(int) ovr_DoSomething(ovrSession session)
//    {
//        int result = 0;
//
//        OVR_CAPI_TRY_VALUE(result)
//        {
//            HMDState* hmds = GetHMDStateFromOvrHmd(session);
//            if (hmds)
//                result = hmds->DoSomething;
//        }
//        OVR_CAPI_CATCH
//
//        return result;
//    }
//
//    OVR_PUBLIC_FUNCTION(ovrResult) ovr_DoSomething(ovrSession session)
//    {
//        OVR_CAPI_TRY_OVRRESULT
//        {
//            HMDState* pState = GetHMDStateFromOvrHmd(session);
//            if (!pState)
//            {
//                OVR_MAKE_ERROR(ovrError_InvalidSession, L"Invalid session.");
//                return ovrError_InvalidSession;
//            }
//            return hmds->DoSomething();
//        }
//        OVR_CAPI_CATCH
//
//        return ovrError_CPPException;
//    }

#if defined(OVR_BUILD_DEBUG)

#define OVR_CAPI_TRY_VOID \
  do {                    \
  } while (false);

#define OVR_CAPI_TRY_VALUE(exceptionStateReturnValue) \
  do {                                                \
  } while (false);

#define OVR_CAPI_TRY_OVRRESULT \
  do {                         \
  } while (false);

#define OVR_CAPI_CATCH \
  do {                 \
  } while (false);

#else // OVR_BUILD_DEBUG

#define OVR_CAPI_TRY_VOID                       \
  if (OVR::gCPPExceptionInfo.ExceptionOccurred) \
    return;                                     \
  try

#define OVR_CAPI_TRY_VALUE(exceptionStateReturnValue) \
  if (OVR::gCPPExceptionInfo.ExceptionOccurred)       \
    return exceptionStateReturnValue;                 \
  try

#define OVR_CAPI_TRY_OVRRESULT                  \
  if (OVR::gCPPExceptionInfo.ExceptionOccurred) \
    return ovrError_RuntimeException;           \
  try

#define OVR_CAPI_CATCH                                                         \
  catch (const std::exception& e) {                                            \
    OVR::gCPPExceptionInfo.FromStdException(e);                                \
  }                                                                            \
  catch (...) {                                                                \
    OVR::gCPPExceptionInfo.FromString("Non-standard C++ exception occurred."); \
  }

#endif // OVR_BUILD_DEBUG

/// -----------------------------------------------------------------------------
/// ***** OVRErrorCallback
///
/// Identifies a callback error handler.
/// Callbacks were added instead of integrating logic directly in OVRError
/// to make application specific logic easier
///
typedef std::function<void(const OVRError& err, bool quiet)> OVRErrorCallback;

/// -----------------------------------------------------------------------------
/// ***** SetErrorCallback
///
/// Sets callback to be issued whenever an error is encountered.
///
void SetErrorCallback(OVRErrorCallback callback);

} // namespace OVR

#ifndef MICRO_OVR
namespace ovrlog {

template <>
void LogStringize(LogStringBuffer& buffer, const OVR::OVRError& error);

} // namespace ovrlog
#endif