fix debug function and log query on output