title: Generate trace templates in Emacs
body: <p>While debugging, I usually write a lot of traces to print variables on screen during the execution. It would be nice if Emacs could write the template for me. I don't know how to program it on Emacs so I'm asking for your help :)</p>
  
  <p>My idea is as follows. Something like M-x trace-template, that will ask for the name of a variable. Then, something like this will be added to the active buffer:</p>
  
  <pre><code>// trace20130604
  cout &lt;&lt; "test.cpp variable: " &lt;&lt; variable &lt;&lt; endl;
  </code></pre>
  
  <p>The date added in the comment allows performing a quick search for the day's traces to remove them when not needed. The "test.cpp" is the active buffer, automatically inserted. And "variable" is the variable's name that the Emacs command asks you when executed.</p>
  
  <p>Do you think this would be useful? Easy to implement? Maybe already implemented? :)</p>
  
  <p>Ideally, it should be mode-specific, so the right template could be inserted depending on the language you are using. </p>
  
  <p>Thanks a lot for your help!</p>
  
  <p>Regards.</p>
  
  <h1>Solution for several major-modes</h1>
  
  <p>I've expanded Francesco's solution to cover for two major-modes. It's the first time I try to do something in Elisp and I suppose the code us ugly but it seems to work.</p>
  
  <pre><code>(defun trace-template (variable-name)
    (interactive "sVariable Name: ")
    (if (string-equal (format "%s" major-mode) "c++-mode")
        (progn
              (insert (format-time-string "// trace%Y%m%d\n"))
              (insert (format "cout &lt;&lt; \"%s %s:\" &lt;&lt; %s &lt;&lt; endl;\n\n"
                    (file-name-nondirectory buffer-file-name)
                    variable-name variable-name)))
      )
    (if (string-equal (format "%s" major-mode) "fortran-mode")
        (progn
              (insert (format-time-string "! trace%Y%m%d\n"))
              (insert (format "WRITE(*,*) \"%s %s:\", %s\n\n"
                    (file-name-nondirectory buffer-file-name)
                    variable-name variable-name)))
      )
    )
  </code></pre>