How do I make proper use of Logging.h?

username: gusetellez
cooked: <p>I am new to PyTorch coding. I am writing algorithms in C++. I would like to log their progress using the logging infrastructure provided with PyTorch. There is code for logging in c10/util/Logging.h. The coding style looks like this:</p>
<p><span class="hashtag">#include</span> &lt;c10/util/Logging.h&gt;<br>
…<br>
VLOG(0) &lt;&lt; “Hello world! \n”;</p>
<p>The above code works, in that it compiles. And no printout is produced.</p>
<p>Details. The code I wrote is added as a module to a PyTorch python script. So the question is how does one manipulate the PyTorch logging infrastructure?</p>
<p>How do I turn on the log printout? How do I control where it goes?</p>
<p>Many thanks for any hints!</p>

username: glaringlee
cooked: <p><a class="mention" href="/u/gusetellez">@gusetellez</a><br>
You can take a look at this and step in:<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/702140758fbee844b0e6304cb8f9c10f3fa1fa3a/c10/util/logging_is_not_google_glog.h#L95" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/702140758fbee844b0e6304cb8f9c10f3fa1fa3a/c10/util/logging_is_not_google_glog.h#L95" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/702140758fbee844b0e6304cb8f9c10f3fa1fa3a/c10/util/logging_is_not_google_glog.h#L95</a></h4>
<pre class="onebox"><code class="lang-h"><ol class="start lines" start="85" style="counter-reset: li-counter 84 ;">
<li>// ---------------------- Logging Macro definitions --------------------------</li>
<li>
</li><li>static_assert(</li>
<li>    CAFFE2_LOG_THRESHOLD &lt;= ::c10::GLOG_FATAL,</li>
<li>    "CAFFE2_LOG_THRESHOLD should at most be GLOG_FATAL.");</li>
<li>// If n is under the compile time caffe log threshold, The _CAFFE_LOG(n)</li>
<li>// should not generate anything in optimized code.</li>
<li>#define LOG(n)                                    \</li>
<li>  if (::c10::GLOG_##n &gt;= CAFFE2_LOG_THRESHOLD) \</li>
<li>  ::c10::MessageLogger((char*)__FILE__, __LINE__, ::c10::GLOG_##n).stream()</li>
<li class="selected">#define VLOG(n)                   \</li>
<li>  if (-n &gt;= CAFFE2_LOG_THRESHOLD) \</li>
<li>  ::c10::MessageLogger((char*)__FILE__, __LINE__, -n).stream()</li>
<li>
</li><li>#define LOG_IF(n, condition)                                     \</li>
<li>  if (::c10::GLOG_##n &gt;= CAFFE2_LOG_THRESHOLD &amp;&amp; (condition)) \</li>
<li>  ::c10::MessageLogger((char*)__FILE__, __LINE__, ::c10::GLOG_##n).stream()</li>
<li>#define VLOG_IF(n, condition)                    \</li>
<li>  if (-n &gt;= CAFFE2_LOG_THRESHOLD &amp;&amp; (condition)) \</li>
<li>  ::c10::MessageLogger((char*)__FILE__, __LINE__, -n).stream()</li>
<li>
</li></ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>But this logging header is mainly for internal use, the underlying is actually very similar to glog, you can use glog directly actually.</p>

username: gusetellez
cooked: <p>Thanks for your reply. I decided to take a very small subset of the code in “logging_is_not_google_glog.h”, and write a tiny logger for my functions.</p>