CUDNN Frontend API 8.3.0

cudnn_frontend_Logging.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #pragma once
24
25 #include <iostream>
26 #include <fstream>
27 #include <cstring>
28
29 #include "cudnn_backend_base.h"
30 namespace cudnn_frontend {
31 inline bool &
32 isLoggingEnabled() {
33 static bool log_enabled = std::getenv("CUDNN_FRONTEND_LOG_INFO") && std::strncmp(std::getenv("CUDNN_FRONTEND_LOG_INFO"), "0",1);
34 return log_enabled;
35 }
36
37
38 inline std::ostream &
39 getStream() {
40 static std::ofstream outFile;
41 static std::ostream & stream = std::getenv("CUDNN_FRONTEND_LOG_FILE")
42 ? (std::strncmp(std::getenv("CUDNN_FRONTEND_LOG_FILE"), "stdout", 6) == 0
43 ? std::cout : (std::strncmp(std::getenv("CUDNN_FRONTEND_LOG_FILE"), "stderr", 6) == 0
44 ? std::cerr : (outFile.open(std::getenv("CUDNN_FRONTEND_LOG_FILE"), std::ios::out), outFile)))
45 : (isLoggingEnabled() = false, std::cout);
46 return stream;
47 }
48
49
50 class ConditionalStreamer {
51 private:
52 std::ostream &stream;
53 public:
54 ConditionalStreamer(std::ostream &stream_) : stream(stream_){}
55
56 template <typename T>
57 const ConditionalStreamer &
58 operator<< (const T &t) const {
59 if (isLoggingEnabled()) {stream << t;}
60 return *this;
61 }
62
63 const ConditionalStreamer &
64 operator<< (std::ostream &(*spl)(std::ostream &)) const {
65 if (isLoggingEnabled()) {stream << spl;}
66 return *this;
67 }
68 };
69
70
71 inline ConditionalStreamer &
72 getLogger() {
73 static ConditionalStreamer opt(getStream());
74 return opt;
75 }
76
77 static
78 std::ostream &
79 operator << (std::ostream &os, const BackendDescriptor & desc) {
80 if (isLoggingEnabled()) {os << desc.describe();}
81 return os;
82 }
83 }
cudnn_frontend::getLogger
ConditionalStreamer & getLogger()
Definition: cudnn_frontend_Logging.h:72
cudnn_frontend::getStream
std::ostream & getStream()
Definition: cudnn_frontend_Logging.h:39
cudnn_frontend::ConditionalStreamer
Definition: cudnn_frontend_Logging.h:50
cudnn_frontend::BackendDescriptor
Definition: cudnn_backend_base.h:99
cudnn_frontend::BackendDescriptor::describe
virtual std::string describe() const =0
Return a string describing the backend Descriptor.
cudnn_frontend::isLoggingEnabled
bool & isLoggingEnabled()
Definition: cudnn_frontend_Logging.h:32
cudnn_frontend::ConditionalStreamer::ConditionalStreamer
ConditionalStreamer(std::ostream &stream_)
Definition: cudnn_frontend_Logging.h:54
cudnn_frontend
Definition: cudnn_backend_base.h:29
cudnn_frontend::ConditionalStreamer::operator<<
const ConditionalStreamer & operator<<(const T &t) const
Definition: cudnn_frontend_Logging.h:58
cudnn_backend_base.h
cudnn_frontend::ConditionalStreamer::stream
std::ostream & stream
Definition: cudnn_frontend_Logging.h:52