From f9ab98e4eb574f2acef46d32493be2bec7f3dcfd Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 1 Mar 2016 23:44:41 +0000 Subject: [PATCH] err backtrace, stronger err in Db, dbq handle php --- bin/dbq | 54 ++++++++++++++++++++++++++----------------- lib/php/db.php | 32 +++++++++++++------------ lib/php/functions.php | 15 ++++++++---- lib/php/nb.php | 31 ++++++++++++++----------- 4 files changed, 77 insertions(+), 55 deletions(-) diff --git a/bin/dbq b/bin/dbq index a2ebcc45..c3e00ebd 100755 --- a/bin/dbq +++ b/bin/dbq @@ -88,24 +88,14 @@ for my $o (@CURL_OPT) { # # Build command # -my $url = ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : 'http://127.0.0.1'); -my @cmd = ( - #'echo', - 'curl', $url, -); - -if ($url =~ m,^\w+://[\d\.]+/?,) { - push (@cmd,'-H', 'Host: '.($ENV{$UC_NAME.'_HOST'} ? $ENV{$UC_NAME.'_HOST'} : 'rent')); -} - -# -# Add args -# -push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-'; +my @cmd; +my $url; +$url = shift @ARGV if @ARGV and ($ARGV[0] =~ m|^\w+://| or -e $ARGV[0] ); # # Distinct arg from key=value # +push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-'; my %keys = (); while ($_ = shift @ARGV) { @@ -146,15 +136,37 @@ if ($keys{format} eq 'nc') { #warn 'Uri: '.join("&",@keys)."\n" if $VERBOSE>1; #warn 'Command:',map{(/^--data/?"\n ":" ").$_} @cmd,"\n" if $VERBOSE; #die join(" ",@cmd); + +# +# Choice http / php +# +$url = ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : '/opt/rent/www/index.php'); +if (-e $url) { + @cmd = ('php','-f',$url); + # Push key=value + push(@cmd,map {$_.'='.$keys{$_}} sort keys %keys); + +} elsif($url) { + @cmd = ('curl', $url, @cmd); + + # Push key=value + push(@cmd,map {('--data-binary',uri_esc($_).'='.uri_esc($keys{$_}))} sort keys %keys); + +# NB 01.03.16 if ($url =~ m,^\w+://[\d\.]+/?,) { +# NB 01.03.16 push (@cmd,'-H', 'Host: '.($ENV{$UC_NAME.'_HOST'} ? $ENV{$UC_NAME.'_HOST'} : 'rent')); +# NB 01.03.16 } + +} else { + + die("$NAME: Please pass the url or the executable or set the ENV ".$UC_NAME.'_URL'); +} + ################################################################################# # # END - Exec command # ################################################################################# -# Push key=value -push(@cmd,map {('--data-binary',uri_esc($_).'='.uri_esc($keys{$_}))} sort keys %keys); - warn "$NAME: Command: ",join(" ",map{/\s+/ ? '"'.$_.'"' : $_} @cmd),"\n" if $VERBOSE; system @cmd; close STDOUT; @@ -168,8 +180,8 @@ exit 0; sub parse_debug { while (<>) { - if (/DEBUG: /) { print STDERR $_ } - elsif ( /^
/) { print STDERR uc($1).': '.html2txt($_) }
+    if (/^(DEBUG|INFO|WARN|ERR|BYE): /) { print STDERR $_ }
+    elsif ( /^
/) { print STDERR uc($1).': '.html2txt($_) }
     elsif ( /^(<\w+>)?Fatal error/ .. /^\s+thrown in/) { print STDERR html2txt($_) }
     elsif ( /^(<\s*\w+\s*\/?>)?(Parse error|Fatal error|Warning|Notice|$)/) { print STDERR $_ if ($_=html2txt($_)) and $_ !~ /^\s*$/ }
     else { print $_ }
@@ -377,7 +389,7 @@ $NAME - Script to query http databases
 
 =head1 SYNOPSIS
 
-Quick usage:
+Quick usage: $NAME [URL|EXEC] [CURL_OPTIONS] [key=value]...
 
 =over
 
@@ -389,7 +401,7 @@ Quick usage:
 
 =head1 DESCRIPTION
 
-Curl wrapper
+Curl / Php query wrapper
 
 =head1 OPTIONS
 
diff --git a/lib/php/db.php b/lib/php/db.php
index 6d2c0428..2f115650 100644
--- a/lib/php/db.php
+++ b/lib/php/db.php
@@ -57,34 +57,36 @@ class db extends nb {
 
   function __construct($db) {
 
+    # Args
     $db = is_scalar($db) ? array('name' => $db) : $db;
     if (is_array($db)) {
       foreach ($db as $k=>$v) $this->$k = $v;
     }
-    $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
-    #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m);
 
-# NB 12.01.16     try {
-
-      if ($this->pdo) {
-        $this->conn = new PDO($this->pdo,$this->username,$this->password,$this->options);
-        if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
-      }
-
-# NB 12.01.16     } catch (PDOException $e) {
-# NB 12.01.16 
-# NB 12.01.16       err($e->getMessage(), "Db.new()");
-# NB 12.01.16       return false;
-# NB 12.01.16 
-# NB 12.01.16     }
+    # Pdo
+    if ($this->pdo) {
+      $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
+      if (!in_array($this->type,array(
+        'sqlite',
+        'mysql',
+        'pgsql',
+      ))) $this->bye("unknow type = ".$this->type);
+      #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m);
+      $this->conn = new PDO($this->pdo,$this->username,$this->password,$this->options);
+      if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
+    }
 
+    # Name
     #if (empty($this->name)) $this->name = preg_replace('/^(?:(?:sqlite:.*(.*?)(\.\w+)?)|(?:.*?dbname=([^;]+).*?))$/','\1',$this->pdo);
 # NB 22.12.15     if (empty($this->name) and preg_match('/(?:sqlite:|dbname=)([^;\.]+)/',$this->pdo,$m)) {
     if (empty($this->name) and preg_match('/(?:sqlite:(?:.*\/)?|dbname=)([^;\.]+)/',$this->pdo,$m)) {
       $this->name = $m[1];
     }
+
+    # Title
     if (empty($this->title)) $this->title = prettyText($this->name);
 
+    # Types
     if ($this->type == 'sqlite') {
 
       $this->conn->sqliteCreateFunction('ip2int', function ($value) { return ip2long($value); });
diff --git a/lib/php/functions.php b/lib/php/functions.php
index fba3b8e0..263f75f5 100644
--- a/lib/php/functions.php
+++ b/lib/php/functions.php
@@ -94,8 +94,8 @@ function txt2md($txt) {
   return $_txt2md->text($txt);
 }
 
-function bye($msg='') {
-  if ($msg) err($msg);
+function bye($msg='',$backtrace_deep=0) {
+  if ($msg) err($msg,'bye',1+$backtrace_deep);
   exit;
 }
 
@@ -103,10 +103,15 @@ function warn ($msg) {
   file_write("php://stderr","$msg\n");
 }
 
-function err($msg) {
+function err($msg,$preff='err',$backtrace_deep=0) {
   $msg = is_scalar($msg) ? $msg : print_r($msg,true);
-  $msg = $msg.' '.nb::debug_backtrace_msg();
-  echo( preg_match('/ml/i',nb::get_header('Content-type')) ? '
'.$msg.'
' : "ERR: $msg").PHP_EOL; + $msg = $msg.' '.nb::debug_backtrace_msg(1+$backtrace_deep,NULL,strtoupper($preff).": "); + + echo( preg_match('/ml/i',nb::get_header('Content-type')) + ? '
'.$msg.'
' + : strtoupper($preff).": $msg" + ).PHP_EOL; + return false; } diff --git a/lib/php/nb.php b/lib/php/nb.php index 77ad2f9f..af16fead 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -204,26 +204,29 @@ class nb { * @author NB * Return backtrace message */ - static function debug_backtrace_msg($deep=NULL,$one_line=NULL) { + static function debug_backtrace_msg($deep=NULL,$one_line=NULL,$preff='') { - $debug = debug_backtrace(); $msg = ''; - array_shift($debug); if (isset($_REQUEST['_debug_backtrace_msg'])) $deep = $_REQUEST['_debug_backtrace_msg']; - if ($deep===NULL or $deep===TRUE) { + #if ($deep===NULL or $deep===TRUE) { - $msg .= ' ['; + #$msg .= "\n["; - foreach (array_reverse($debug) as $i=>$call_info) - $msg .= ( $one_line ? ($i>0 ? ", " : "") : "\n\t" ) . nb::debug_backtrace_info($call_info); + $debug = debug_backtrace(); array_shift($debug); - $msg .= ($one_line ? "" : "\n")."]"; + $tot = count($debug); + foreach (array_reverse($debug) as $i=>$call_info) { + $msg .= ( $one_line ? ($preff !=='' ? $preff : ' | ') : "\n$preff " ) . nb::debug_backtrace_info($call_info); + if ($deep and ($tot-$i) <= $deep+1) break; + } - } elseif (isset($call_info[(int)$deep])) { - $msg .= ' '.nb::debug_backtrace_info($call_info[(int)$deep]); + #$msg .= ($one_line ? "" : "\n")."]"; - } + #} elseif (isset($call_info[(int)$deep])) { + #$msg .= ' '.nb::debug_backtrace_info($call_info[(int)$deep]); + + #} return $msg; } @@ -261,7 +264,9 @@ class nb { #else ($call_info['type']) $call_info['type'] = $call_info['type']; // Default values - if (isset($call_info['file'])) $call_info['file'] = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$call_info['file']); + if (isset($call_info['file']) and !empty($_SERVER['DOCUMENT_ROOT'])) + $call_info['file'] = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$call_info['file']) + ; // Get infos $msg = array(); @@ -269,13 +274,11 @@ class nb { 'file', 'line', 'function', - # NB 21.07.11 'type', 'type', 'message', 'code', ) as $i) { - die($i); if (array_key_exists($i,$call_info)) $msg[] = "$i=".$call_info[$i]; } -- 2.47.3