8daf79cb0c8e3d37fb0fb76ac40c8c5cdf176ca6.svn-base 41.4 KB
<!DOCTYPE html>  <html> <head>   <title>file.js</title>   <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <link rel="stylesheet" media="all" href="../../docco.css" /> </head> <body>   <div id="container">     <div id="background"></div>            <div id="jump_to">         Jump To &hellip;         <div id="jump_wrapper">           <div id="jump_page">                                           <a class="source" href="../..//winston/common.html">                 winston/common.html               </a>                                           <a class="source" href="../..//winston/config/cli-config.html">                 winston/config/cli-config.html               </a>                                           <a class="source" href="../..//winston/config/npm-config.html">                 winston/config/npm-config.html               </a>                                           <a class="source" href="../..//winston/config/syslog-config.html">                 winston/config/syslog-config.html               </a>                                           <a class="source" href="../..//winston/config.html">                 winston/config.html               </a>                                           <a class="source" href="../..//winston/exception.html">                 winston/exception.html               </a>                                           <a class="source" href="../..//winston/logger.html">                 winston/logger.html               </a>                                           <a class="source" href="../..//winston/transports/console.html">                 winston/transports/console.html               </a>                                           <a class="source" href="../..//winston/transports/couchdb.html">                 winston/transports/couchdb.html               </a>                                           <a class="source" href="../..//winston/transports/file.html">                 winston/transports/file.html               </a>                                           <a class="source" href="../..//winston/transports/loggly.html">                 winston/transports/loggly.html               </a>                                           <a class="source" href="../..//winston/transports/transport.html">                 winston/transports/transport.html               </a>                                           <a class="source" href="../..//winston/transports/webhook.html">                 winston/transports/webhook.html               </a>                                           <a class="source" href="../..//winston/transports.html">                 winston/transports.html               </a>                                           <a class="source" href="../..//winston.html">                 winston.html               </a>                        </div>         </div>       </div>          <table cellpadding="0" cellspacing="0">       <thead>         <tr>           <th class="docs">             <h1>               file.js             </h1>           </th>           <th class="code">           </th>         </tr>       </thead>       <tbody>                               <tr id="section-1">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-1">&#182;</a>               </div>                            </td>             <td class="code">               <div class="highlight"><pre><span class="cm">/*</span>
<span class="cm"> * file.js: Transport for outputting to a local log file</span>
<span class="cm"> *</span>
<span class="cm"> * (C) 2010 Charlie Robbins</span>
<span class="cm"> * MIT LICENCE</span>
<span class="cm"> *</span>
<span class="cm"> */</span>

<span class="kd">var</span> <span class="nx">events</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;events&#39;</span><span class="p">),</span>
    <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;fs&#39;</span><span class="p">),</span>
    <span class="nx">path</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;path&#39;</span><span class="p">),</span>
    <span class="nx">util</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;util&#39;</span><span class="p">),</span>
    <span class="nx">colors</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;colors&#39;</span><span class="p">),</span>
    <span class="nx">common</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;../common&#39;</span><span class="p">),</span>
    <span class="nx">Transport</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./transport&#39;</span><span class="p">).</span><span class="nx">Transport</span><span class="p">;</span>
    </pre></div>             </td>           </tr>                               <tr id="section-2">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-2">&#182;</a>               </div>               <h3>function File (options)</h3>

<h4>@options {Object} Options for this instance.</h4>

<p>Constructor function for the File transport object responsible
for persisting log messages and metadata to one or more files.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="kd">var</span> <span class="nx">File</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">File</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">Transport</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span>
  </pre></div>             </td>           </tr>                               <tr id="section-3">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-3">&#182;</a>               </div>               <p>Helper function which throws an <code>Error</code> in the event
that any of the rest of the arguments is present in <code>options</code>. </p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">function</span> <span class="nx">throwIf</span> <span class="p">(</span><span class="nx">target</span> <span class="cm">/*, illegal... */</span><span class="p">)</span> <span class="p">{</span>
    <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">[</span><span class="nx">name</span><span class="p">])</span> <span class="p">{</span>
        <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;Cannot set &#39;</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="s1">&#39; and &#39;</span> <span class="o">+</span> <span class="nx">target</span> <span class="o">+</span> <span class="s1">&#39;together&#39;</span><span class="p">);</span>
      <span class="p">}</span>
    <span class="p">});</span>
  <span class="p">}</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">filename</span> <span class="o">||</span> <span class="nx">options</span><span class="p">.</span><span class="nx">dirname</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">throwIf</span><span class="p">(</span><span class="s1">&#39;filename or dirname&#39;</span><span class="p">,</span> <span class="s1">&#39;stream&#39;</span><span class="p">);</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">_basename</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">filename</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">filename</span><span class="p">)</span> <span class="o">||</span> <span class="s1">&#39;winston.log&#39;</span><span class="p">;</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">dirname</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">dirname</span> <span class="o">||</span> <span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">filename</span><span class="p">);</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">options</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">options</span> <span class="o">||</span> <span class="p">{</span> <span class="nx">flags</span><span class="o">:</span> <span class="s1">&#39;a&#39;</span> <span class="p">};</span>    
  <span class="p">}</span>
  <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">stream</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">throwIf</span><span class="p">(</span><span class="s1">&#39;stream&#39;</span><span class="p">,</span> <span class="s1">&#39;filename&#39;</span><span class="p">,</span> <span class="s1">&#39;maxsize&#39;</span><span class="p">);</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">stream</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">stream</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="k">else</span> <span class="p">{</span>
    <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;Cannot log to file without filename or stream.&#39;</span><span class="p">);</span>
  <span class="p">}</span>
    
  <span class="k">this</span><span class="p">.</span><span class="nx">json</span>      <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">json</span> <span class="o">!==</span> <span class="kc">false</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">colorize</span>  <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">colorize</span>  <span class="o">||</span> <span class="kc">false</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">maxsize</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">maxsize</span>   <span class="o">||</span> <span class="kc">null</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">timestamp</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">options</span><span class="p">.</span><span class="nx">timestamp</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">?</span> <span class="nx">options</span><span class="p">.</span><span class="nx">timestamp</span> <span class="o">:</span> <span class="kc">false</span><span class="p">;</span></pre></div>             </td>           </tr>                               <tr id="section-4">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-4">&#182;</a>               </div>               <p>Internal state variables representing the number
of files this instance has created and the current
size (in bytes) of the current logfile.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="k">this</span><span class="p">.</span><span class="nx">_size</span>    <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">_created</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">_buffer</span>  <span class="o">=</span> <span class="p">[];</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-5">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-5">&#182;</a>               </div>               <p>Inherit from <code>winston.Transport</code>.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">util</span><span class="p">.</span><span class="nx">inherits</span><span class="p">(</span><span class="nx">File</span><span class="p">,</span> <span class="nx">Transport</span><span class="p">);</span></pre></div>             </td>           </tr>                               <tr id="section-6">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-6">&#182;</a>               </div>               <p>Expose the name of this Transport on the prototype</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">File</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s1">&#39;file&#39;</span><span class="p">;</span></pre></div>             </td>           </tr>                               <tr id="section-7">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-7">&#182;</a>               </div>               <h3>function log (level, msg, [meta], callback)</h3>

<h4>@level {string} Level at which to log the message.</h4>

<h4>@msg {string} Message to log</h4>

<h4>@meta {Object} <strong>Optional</strong> Additional metadata to attach</h4>

<h4>@callback {function} Continuation to respond to when complete.</h4>

<p>Core logging method exposed to Winston. Metadata is optional.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">File</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">log</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">level</span><span class="p">,</span> <span class="nx">msg</span><span class="p">,</span> <span class="nx">meta</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">silent</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span> <span class="nx">output</span> <span class="o">=</span> <span class="nx">common</span><span class="p">.</span><span class="nx">log</span><span class="p">({</span>
    <span class="nx">level</span><span class="o">:</span>     <span class="nx">level</span><span class="p">,</span>
    <span class="nx">message</span><span class="o">:</span>   <span class="nx">msg</span><span class="p">,</span>
    <span class="nx">meta</span><span class="o">:</span>      <span class="nx">meta</span><span class="p">,</span>
    <span class="nx">json</span><span class="o">:</span>      <span class="k">this</span><span class="p">.</span><span class="nx">json</span><span class="p">,</span>
    <span class="nx">colorize</span><span class="o">:</span>  <span class="k">this</span><span class="p">.</span><span class="nx">colorize</span><span class="p">,</span>
    <span class="nx">timestamp</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">timestamp</span>
  <span class="p">})</span> <span class="o">+</span> <span class="s1">&#39;\n&#39;</span><span class="p">;</span>
  
  <span class="k">this</span><span class="p">.</span><span class="nx">_size</span> <span class="o">+=</span> <span class="nx">output</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
  
  <span class="kd">function</span> <span class="nx">onDrain</span> <span class="p">()</span> <span class="p">{</span>
    <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;logged&#39;</span><span class="p">);</span>
  <span class="p">}</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">filename</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-8">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-8">&#182;</a>               </div>               <p>If there is no <code>filename</code> on this instance then it was configured
with a raw <code>WriteableStream</code> instance and we should not perform any
size restrictions.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="k">this</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">output</span><span class="p">);</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">once</span><span class="p">(</span><span class="s1">&#39;drain&#39;</span><span class="p">,</span> <span class="nx">onDrain</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="k">else</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-9">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-9">&#182;</a>               </div>               <p>If there was an error enqueue the message</p>             </td>             <td class="code">               <div class="highlight"><pre>        <span class="k">return</span> <span class="nx">self</span><span class="p">.</span><span class="nx">_buffer</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">output</span><span class="p">);</span>
      <span class="p">}</span>
      
      <span class="nx">self</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">output</span><span class="p">);</span>
      <span class="nx">self</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">once</span><span class="p">(</span><span class="s1">&#39;drain&#39;</span><span class="p">,</span> <span class="nx">onDrain</span><span class="p">);</span>
    <span class="p">});</span>
  <span class="p">}</span>

  <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-10">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-10">&#182;</a>               </div>               <h3>function open (callback)</h3>

<h4>@callback {function} Continuation to respond to when complete</h4>

<p>Checks to see if a new file needs to be created based on the <code>maxsize</code>
(if any) and the current size of the file used.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">File</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">open</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">opening</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-11">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-11">&#182;</a>               </div>               <p>If we are already attempting to open the next
available file then respond with a value indicating
that the message should be buffered.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">stream</span> <span class="o">||</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">maxsize</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="p">.</span><span class="nx">_size</span> <span class="o">&gt;=</span> <span class="k">this</span><span class="p">.</span><span class="nx">maxsize</span><span class="p">))</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-12">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-12">&#182;</a>               </div>               <p>If we dont have a stream or have exceeded our size, then create
the next stream and respond with a value indicating that 
the message should be buffered.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">callback</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
    <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_createStream</span><span class="p">();</span>
  <span class="p">}</span>
  </pre></div>             </td>           </tr>                               <tr id="section-13">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-13">&#182;</a>               </div>               <p>Otherwise we have a valid (and ready) stream.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">callback</span><span class="p">();</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-14">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-14">&#182;</a>               </div>               <h3>function close ()</h3>

<p>Closes the stream associated with this instance.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">File</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">close</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>

  <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">stream</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">destroySoon</span><span class="p">();</span>
    
    <span class="k">this</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">once</span><span class="p">(</span><span class="s1">&#39;drain&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
      <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;flush&#39;</span><span class="p">);</span>
      <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;closed&#39;</span><span class="p">);</span>
    <span class="p">});</span>
  <span class="p">}</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-15">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-15">&#182;</a>               </div>               <h3>function flush ()</h3>

<p>Flushes any buffered messages to the current <code>stream</code>
used by this instance.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">File</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">flush</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span></pre></div>             </td>           </tr>                               <tr id="section-16">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-16">&#182;</a>               </div>               <p>Iterate over the <code>_buffer</code> of enqueued messaged
and then write them to the newly created stream. </p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="k">this</span><span class="p">.</span><span class="nx">_buffer</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">process</span><span class="p">.</span><span class="nx">nextTick</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
      <span class="nx">self</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">str</span><span class="p">);</span>
      <span class="nx">self</span><span class="p">.</span><span class="nx">_size</span> <span class="o">+=</span> <span class="nx">str</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
    <span class="p">});</span>
  <span class="p">});</span>
  </pre></div>             </td>           </tr>                               <tr id="section-17">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-17">&#182;</a>               </div>               <p>Quickly truncate the <code>_buffer</code> once the write operations
have been started</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">self</span><span class="p">.</span><span class="nx">_buffer</span><span class="p">.</span><span class="nx">length</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  </pre></div>             </td>           </tr>                               <tr id="section-18">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-18">&#182;</a>               </div>               <p>When the stream has drained we have flushed
our buffer.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">self</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">once</span><span class="p">(</span><span class="s1">&#39;drain&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
    <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;flush&#39;</span><span class="p">);</span>
    <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;logged&#39;</span><span class="p">);</span>
  <span class="p">});</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-19">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-19">&#182;</a>               </div>               <h3>@private function _createStream ()</h3>

<p>Attempts to open the next appropriate file for this instance
based on the common state (such as <code>maxsize</code> and <code>_basename</code>).</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">File</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">_createStream</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">opening</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
    
  <span class="p">(</span><span class="kd">function</span> <span class="nx">checkFile</span> <span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">fullname</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">dirname</span><span class="p">,</span> <span class="nx">target</span><span class="p">);</span>
    </pre></div>             </td>           </tr>                               <tr id="section-20">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-20">&#182;</a>               </div>               <p>Creates the <code>WriteStream</code> and then flushes any
buffered messages.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="kd">function</span> <span class="nx">createAndFlush</span> <span class="p">(</span><span class="nx">size</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">stream</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">self</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
        <span class="nx">self</span><span class="p">.</span><span class="nx">stream</span><span class="p">.</span><span class="nx">destroySoon</span><span class="p">();</span>
      <span class="p">}</span>
      
      <span class="nx">self</span><span class="p">.</span><span class="nx">_size</span> <span class="o">=</span> <span class="nx">size</span><span class="p">;</span>
      <span class="nx">self</span><span class="p">.</span><span class="nx">filename</span> <span class="o">=</span> <span class="nx">target</span><span class="p">;</span>
      <span class="nx">self</span><span class="p">.</span><span class="nx">stream</span> <span class="o">=</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">createWriteStream</span><span class="p">(</span><span class="nx">fullname</span><span class="p">,</span> <span class="nx">self</span><span class="p">.</span><span class="nx">options</span><span class="p">);</span>
      </pre></div>             </td>           </tr>                               <tr id="section-21">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-21">&#182;</a>               </div>               <p>When the current stream has finished flushing 
then we can be sure we have finished opening 
and thus can emit the <code>open</code> event.</p>             </td>             <td class="code">               <div class="highlight"><pre>      <span class="nx">self</span><span class="p">.</span><span class="nx">once</span><span class="p">(</span><span class="s1">&#39;flush&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="nx">self</span><span class="p">.</span><span class="nx">opening</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
        <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;open&#39;</span><span class="p">,</span> <span class="nx">fullname</span><span class="p">);</span>
      <span class="p">});</span></pre></div>             </td>           </tr>                               <tr id="section-22">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-22">&#182;</a>               </div>               <p>Remark: It is possible that in the time it has taken to find the
next logfile to be written more data than <code>maxsize</code> has been buffered,
but for sensible limits (10s - 100s of MB) this seems unlikely in less
than one second.</p>             </td>             <td class="code">               <div class="highlight"><pre>      <span class="nx">self</span><span class="p">.</span><span class="nx">flush</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="nx">fs</span><span class="p">.</span><span class="nx">stat</span><span class="p">(</span><span class="nx">fullname</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">stats</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">.</span><span class="nx">code</span> <span class="o">!==</span> <span class="s1">&#39;ENOENT&#39;</span><span class="p">)</span> <span class="p">{</span>
          <span class="k">return</span> <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">,</span> <span class="nx">err</span><span class="p">);</span>
        <span class="p">}</span>
        
        <span class="k">return</span> <span class="nx">createAndFlush</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
      <span class="p">}</span>
      
      <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">stats</span> <span class="o">||</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">maxsize</span> <span class="o">&amp;&amp;</span> <span class="nx">stats</span><span class="p">.</span><span class="nx">size</span> <span class="o">&gt;=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">maxsize</span><span class="p">))</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-23">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-23">&#182;</a>               </div>               <p>If <code>stats.size</code> is greater than the <code>maxsize</code> for 
this instance then try again </p>             </td>             <td class="code">               <div class="highlight"><pre>        <span class="k">return</span> <span class="nx">checkFile</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">_getFile</span><span class="p">(</span><span class="kc">true</span><span class="p">));</span>
      <span class="p">}</span>
      
      <span class="nx">createAndFlush</span><span class="p">(</span><span class="nx">stats</span><span class="p">.</span><span class="nx">size</span><span class="p">);</span>
    <span class="p">});</span>
  <span class="p">})(</span><span class="k">this</span><span class="p">.</span><span class="nx">_getFile</span><span class="p">());</span>  
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-24">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-24">&#182;</a>               </div>               <h3>@private function _getFile ()</h3>

<p>Gets the next filename to use for this instance
in the case that log filesizes are being capped.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">File</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">_getFile</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">inc</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
      <span class="nx">ext</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">extname</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_basename</span><span class="p">),</span>
      <span class="nx">basename</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_basename</span><span class="p">,</span> <span class="nx">ext</span><span class="p">);</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="nx">inc</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-25">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-25">&#182;</a>               </div>               <p>Increment the number of files created or 
checked by this instance.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="k">this</span><span class="p">.</span><span class="nx">_created</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
  <span class="p">}</span>
  
  <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_created</span> 
    <span class="o">?</span> <span class="nx">basename</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">_created</span> <span class="o">+</span> <span class="nx">ext</span>
    <span class="o">:</span> <span class="nx">basename</span> <span class="o">+</span> <span class="nx">ext</span><span class="p">;</span>
<span class="p">};</span>

</pre></div>             </td>           </tr>                </tbody>     </table>   </div> </body> </html>