HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sysutil.h
Go to the documentation of this file.
1 // Copyright 2008-present Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/OpenImageIO/oiio
4 
5 
6 /////////////////////////////////////////////////////////////////////////
7 /// @file sysutil.h
8 ///
9 /// @brief Platform-independent utilities for various OS, hardware, and
10 /// system resource functionality, all in namespace Sysutil.
11 /////////////////////////////////////////////////////////////////////////
12 
13 
14 #pragma once
15 
16 #include <ctime>
17 #include <string>
18 
19 #ifdef __MINGW32__
20 # include <malloc.h> // for alloca
21 #endif
22 
23 #include <OpenImageIO/export.h>
25 #include <OpenImageIO/platform.h>
27 
28 // Allow client software to know if this version has Sysutil::stacktrace().
29 #define OIIO_HAS_STACKTRACE 1
30 
31 
32 
34 
35 /// @namespace Sysutil
36 ///
37 /// @brief Platform-independent utilities for various OS, hardware, and
38 /// system resource functionality.
39 namespace Sysutil {
40 
41 /// The amount of memory currently being used by this process, in bytes.
42 /// If resident==true (the default), it will report just the resident
43 /// set in RAM; if resident==false, it returns the full virtual arena
44 /// (which can be misleading because gcc allocates quite a bit of
45 /// virtual, but not actually resident until malloced, memory per
46 /// thread).
47 OIIO_API size_t
48 memory_used(bool resident = true);
49 
50 /// The amount of physical RAM on this machine, in bytes.
51 /// If it can't figure it out, it will return 0.
52 OIIO_API size_t
54 
55 /// Convert calendar time pointed by 'time' into local time and save it in
56 /// 'converted_time' variable. This is a fully reentrant/thread-safe
57 /// alternative to the non-reentrant C localtime() call.
58 OIIO_API void
59 get_local_time(const time_t* time, struct tm* converted_time);
60 
61 /// Return the full path of the currently-running executable program.
62 ///
65 
66 /// Return the value of an environment variable, or if it is not found in
67 /// the environment, return `defaultval`, which in turn defaults to the
68 /// empty string.
70 getenv(string_view name, string_view defaultval);
71 
72 // Legacy for link compatibility. DEPRECATED(2.3)
74 getenv(string_view name);
75 
76 /// Sleep for the given number of microseconds.
77 ///
78 OIIO_API void
79 usleep(unsigned long useconds);
80 
81 /// Try to put the process into the background so it doesn't continue to
82 /// tie up any shell that it was launched from. The arguments are the
83 /// argc/argv that describe the program and its command line arguments.
84 /// Return true if successful, false if it was unable to do so.
85 OIIO_API bool
86 put_in_background(int argc, char* argv[]);
87 
88 /// Number of virtual cores available on this platform (including
89 /// hyperthreads).
90 OIIO_API unsigned int
92 
93 /// Number of full hardware cores available on this platform (does not
94 /// include hyperthreads). This is not always accurate and on some
95 /// platforms will return the number of virtual cores.
96 OIIO_API unsigned int
98 
99 /// Get the maximum number of open file handles allowed on this system.
100 OIIO_API size_t
102 
103 /// Return a string containing a readable stack trace from the point where
104 /// it was called. Return an empty string if not supported on this platform.
106 stacktrace();
107 
108 /// Turn on automatic stacktrace dump to the named file if the program
109 /// crashes. Return true if this is properly set up, false if it is not
110 /// possible on this platform. The name may be "stdout" or "stderr" to
111 /// merely print the trace to stdout or stderr, respectively. If the name
112 /// is "", it will disable the auto-stacktrace printing.
113 OIIO_API bool
115 
116 /// Try to figure out how many columns wide the terminal window is. May not
117 /// be correct on all systems, will default to 80 if it can't figure it out.
118 OIIO_API int
120 
121 /// Try to figure out how many rows tall the terminal window is. May not be
122 /// correct on all systems, will default to 24 if it can't figure it out.
123 OIIO_API int
124 terminal_rows();
125 
126 
127 /// Term object encapsulates information about terminal output for the sake
128 /// of constructing ANSI escape sequences.
129 class OIIO_API Term {
130 public:
131  /// Default ctr: assume ANSI escape sequences are ok.
132  Term() noexcept {}
133  /// Construct from a FILE*: ANSI codes ok if the file describes a
134  /// live console, otherwise they will be supressed.
135  Term(FILE* file);
136  /// Construct from a stream: ANSI codes ok if the file describes a
137  /// live console, otherwise they will be supressed.
138  Term(const std::ostream& stream);
139 
140  /// ansi("appearance") returns the ANSI escape sequence for the named
141  /// command (if ANSI codes are ok, otherwise it will return the empty
142  /// string). Accepted commands include: "default", "bold", "underscore",
143  /// "blink", "reverse", "concealed", "black", "red", "green", "yellow",
144  /// "blue", "magenta", "cyan", "white", "black_bg", "red_bg",
145  /// "green_bg", "yellow_bg", "blue_bg", "magenta_bg", "cyan_bg",
146  /// "white_bg". Commands may be combined with "," for example:
147  /// "bold,green,white_bg".
148  std::string ansi(string_view command) const;
149 
150  /// ansi("appearance", "text") returns the text, with the formatting
151  /// command, then the text, then the formatting command to return to
152  /// default appearance.
154  {
155  return std::string(ansi(command)) + std::string(text) + ansi("default");
156  }
157 
158  /// Extended color control: take RGB values from 0-255
159  std::string ansi_fgcolor(int r, int g, int b);
160  std::string ansi_bgcolor(int r, int g, int b);
161 
162  bool is_console() const noexcept { return m_is_console; }
163 
164 private:
165  bool m_is_console = true; // Default: assume ANSI escape sequences ok.
166 };
167 
168 
169 } // namespace Sysutil
170 
OIIO_API unsigned int hardware_concurrency()
GLuint GLuint stream
Definition: glcorearb.h:1832
OIIO_API void usleep(unsigned long useconds)
GT_API const UT_StringHolder filename
Term() noexcept
Default ctr: assume ANSI escape sequences are ok.
Definition: sysutil.h:132
GT_API const UT_StringHolder time
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
bool is_console() const noexcept
Definition: sysutil.h:162
GLboolean GLboolean g
Definition: glcorearb.h:1222
OIIO_API unsigned int physical_concurrency()
OIIO_API void get_local_time(const time_t *time, struct tm *converted_time)
OIIO_API std::string this_program_path()
OIIO_API size_t memory_used(bool resident=true)
OIIO_API std::string stacktrace()
OIIO_API int terminal_columns()
GLuint const GLchar * name
Definition: glcorearb.h:786
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
std::string ansi(string_view command, string_view text) const
Definition: sysutil.h:153
OIIO_API bool put_in_background(int argc, char *argv[])
OIIO_API bool setup_crash_stacktrace(string_view filename)
OIIO_API string_view getenv(string_view name, string_view defaultval)
OIIO_API size_t physical_memory()
GLboolean r
Definition: glcorearb.h:1222
#define const
Definition: zconf.h:214
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:94
GLint GLint GLint GLint GLsizei GLsizei GLsizei GLboolean resident
Definition: glcorearb.h:3386
OIIO_API size_t max_open_files()
Get the maximum number of open file handles allowed on this system.
OIIO_API int terminal_rows()
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:93
#define OIIO_API
Definition: export.h:65