Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 16 total  RSS 

Using ruby-debug

Uses ruby-debug to step through a script at run-time. keywords: Debugger.start, debug_method

...

require 'ruby-debug'

class ProjectX_handler
  def invoke(project, method, params)
    Debugger.start {
    h = Hash.new
    h['car_log'] = Car_log_handler.new
    h['recordx'] = RecordX_handler.new
    h['mrecord'] = MRecord_handler.new
    h['fileop'] = FileOp_handler.new
    h['rx2projectx'] = RX2ProjectX_handler.new
    h['blog'] = Blog_handler.new
  
    h[project].invoke(method, params)
    }
  end
  debug_method :invoke
end

...

at the (rdb:1) prompt you can type help for information on debugger commands. Commands I've tried so far are
'step' (move to the next statement),
list (show the current line of execution and the fragment of code which the debugger is current analysing),
'break projectx_handler.rb:29' (go to the following line within the file specified, when execution is continued,
'continue' (resume normal code execution),
'v l' (display local variables), and
'p' (evaluate expression and print it's value)

Note: Ruby-debug would not install properly on *Ubuntu 7.10, but installed fine on Ubuntu 7.4 and Gentoo 2007.1.
* - extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:1

*update 12 Feb*
found out that I needed the Ruby dev header files to install the Ruby-debug gem. Here's how sudo apt-get install ruby1.8-dev

dbg - debuging routine

A handy little routine to debug autoit scripts

dbg("Log this message")
Func dbg($msg, $error=@error, $extended=@extended, $ScriptLineNumber=@ScriptLineNumber)
    Local $out = "(" & $ScriptLineNumber & ")(" & $error & ")(" & $extended & ") := " & $msg 
    ;Output to application attaching a console to the script engine
    ConsoleWrite($msg & @CRLF)
    ;Output to debugger (dbgview.exe)
    DllCall("kernel32.dll", "none", "OutputDebugString", "str", $out)
EndFunc

PHP Debugging

// Place at the top of script being developed

error_reporting(E_ALL);

JavaScript var_dump (Mark 2)

Same as var_dump for PHP, but for JavaScript. Useful if you do not have Firebug.

A typical useage:

document.write(var_dump(ANY-JS-VAR,'html'));

     function var_dump(data,addwhitespace,safety,level) {
        var rtrn = '';
        var dt,it,spaces = '';
        if(!level) {level = 1;}
        for(var i=0; i<level; i++) {
           spaces += '   ';
        }//end for i<level
        if(typeof(data) != 'object') {
           dt = data;
           if(typeof(data) == 'string') {
              if(addwhitespace == 'html') {
                 dt = dt.replace(/&/g,'&amp;');
                 dt = dt.replace(/>/g,'&gt;');
                 dt = dt.replace(/</g,'&lt;');
              }//end if addwhitespace == html
              dt = dt.replace(/\"/g,'\"');
              dt = '"' + dt + '"';
           }//end if typeof == string
           if(typeof(data) == 'function' && addwhitespace) {
              dt = new String(dt).replace(/\n/g,"\n"+spaces);
              if(addwhitespace == 'html') {
                 dt = dt.replace(/&/g,'&amp;');
                 dt = dt.replace(/>/g,'&gt;');
                 dt = dt.replace(/</g,'&lt;');
              }//end if addwhitespace == html
           }//end if typeof == function
           if(typeof(data) == 'undefined') {
              dt = 'undefined';
           }//end if typeof == undefined
           if(addwhitespace == 'html') {
              if(typeof(dt) != 'string') {
                 dt = new String(dt);
              }//end typeof != string
              dt = dt.replace(/ /g,"&nbsp;").replace(/\n/g,"<br>");
           }//end if addwhitespace == html
           return dt;
        }//end if typeof != object && != array
        for (var x in data) {
           if(safety && (level > safety)) {
              dt = '*RECURSION*';
           } else {
              try {
                 dt = var_dump(data[x],addwhitespace,safety,level+1);
              } catch (e) {continue;}
           }//end if-else level > safety
           it = var_dump(x,addwhitespace,safety,level+1);
           rtrn += it + ':' + dt + ',';
           if(addwhitespace) {
              rtrn += '\n'+spaces;
           }//end if addwhitespace
        }//end for...in
        if(addwhitespace) {
           rtrn = '{\n' + spaces + rtrn.substr(0,rtrn.length-(2+(level*3))) + '\n' + spaces.substr(0,spaces.length-3) + '}';
        } else {
           rtrn = '{' + rtrn.substr(0,rtrn.length-1) + '}';
        }//end if-else addwhitespace
        if(addwhitespace == 'html') {
           rtrn = rtrn.replace(/ /g,"&nbsp;").replace(/\n/g,"<br>");
        }//end if addwhitespace == html
        return rtrn;
     }//end function var_dump

Debug logs in the database

How to log things in the database

Create the table:
create table TMP_LOG (HORA date, ENTRADA varchar(4000));

Write the things I need to the table:
INSERT INTO TMP_LOG (HORA, ENTRADA) VALUES(SYSDATE, 
  'V_NOM_DESTINATARIO1: '||V_NOM_DESTINATARIO1||', V_DOM_DESTINATARIO1: '||V_DOM_DESTINATARIO1||
  ', P_NOM_DESTINATARIO_2: '||P_NOM_DESTINATARIO_2||', P_DOM_DESTINATARIO_2: '||P_DOM_DESTINATARIO_2||
  ', B_DESTINATARIO: '||B_DESTINATARIO||
  ', V_NOM_DESTINATARIO2: '||V_NOM_DESTINATARIO2||', V_DOM_DESTINATARIO2: '||V_DOM_DESTINATARIO2);


Consult the things written:
select * from tmp_log order by hora desc;

Dont forget to drop it at the end:
drop table TMP_LOG;

Java: log4j Initialization Example

// Initialization for Basic Console Output
// Ref: http://logging.apache.org/log4j/docs/manual.html

 import com.foo.Bar;

 // Import log4j classes.
 import org.apache.log4j.Logger;
 import org.apache.log4j.BasicConfigurator;

 public class MyApp {

   // Define a static logger variable so that it references the
   // Logger instance named "MyApp".
   static Logger logger = Logger.getLogger(MyApp.class);

   public static void main(String[] args) {

     // Set up a simple configuration that logs on the console.
     BasicConfigurator.configure();

     logger.setLevel(Level.DEBUG); // optional if log4j.properties file not used
     // Possible levels: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL

     logger.info("Entering application.");
     Bar bar = new Bar();
     bar.doIt();
     logger.info("Exiting application.");
   }
 }

Java DOM: Printing the Content of a Node

    try
    {
      // Set up the output transformer
      TransformerFactory transfac = TransformerFactory.newInstance();
      Transformer trans = transfac.newTransformer();
      trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
      trans.setOutputProperty(OutputKeys.INDENT, "yes");

      // Print the DOM node

      StringWriter sw = new StringWriter();
      StreamResult result = new StreamResult(sw);
      DOMSource source = new DOMSource(node);
      trans.transform(source, result);
      String xmlString = sw.toString();

      System.out.println(xmlString);
    }
    catch (TransformerException e)
    {
      e.printStackTrace();
    }

Mostar los resultados de un cursor

Esto muestra los resultados de un cursor en la ventana de pruebas del SQL Developer

Declaración de las variables
c1 varchar2(200);
c2 varchar2(200);
c3 varchar2(200);


Código en sí
fetch v_Return into c1, c2, c3;
loop
  EXIT WHEN v_Return%NOTFOUND;
  DBMS_OUTPUT.PUT_LINE(c1||', '||c2||', '||c3);
  fetch v_Return into c1, c2, c3;
end loop;
close v_Return;


Ejemplo del código completo:
DECLARE
  PVCOD_DESPACHO NUMBER;
  PVCOD_IDIOMA VARCHAR2(200);
  v_Return PG_SGIE_Types.cursor_type;
  c1 varchar2(200);
  c2 varchar2(200);
  c3 varchar2(200);
BEGIN
  PVCOD_DESPACHO := 2792;
  PVCOD_IDIOMA := '1';

  v_Return := FU_LISTAENVIOSDESPACHO(
    PVCOD_DESPACHO => PVCOD_DESPACHO,
    PVCOD_IDIOMA => PVCOD_IDIOMA
  );
  -- Modify the code to output the variable
  --DBMS_OUTPUT.PUT_LINE('v_Return = ' || v_Return);
  
  fetch v_Return into c1, c2, c3;
  loop
    EXIT WHEN v_Return%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(c1||', '||c2||', '||c3);
    fetch v_Return into c1, c2, c3;
  end loop;
  close v_Return;
END;

Which class file is loaded by the classloader ?

When using lots of third-party libraries, one problem might be that 2 of them package different versions of the same class, producing errors when method version conflicts happen.

Here is a simple way to know the exact location used by the classloader to get your class :

URL myClassURL = MyMysteryClass.class.getProtectionDomain().getCodeSource().getLocation();

Tomcat debug

// description of your code here
Parametre de conf pour lancer un tomcat en debug afin de se connecter a distance sur le port 8000
Xdebug -Xrunjdwp:transport=dt_socket,adress=8000,server=y,suspend=n
« Newer Snippets
Older Snippets »
Showing 1-10 of 16 total  RSS