Browse Source

Merge pull request #21 from Zjmainstay/dev-multi_force_client_ids_support

多个强制推送client_ids支持
luofei 9 years ago
parent
commit
0bee1b0c81
2 changed files with 43 additions and 16 deletions
  1. 13 6
      demo.php
  2. 30 10
      php/slog.php

+ 13 - 6
demo.php

@@ -3,12 +3,19 @@ include './php/slog.function.php';
 
 //配置
 slog(array(
-'host'=>'localhost',//websocket服务器地址,默认localhost
-'optimize'=>false,//是否显示利于优化的参数,如果运行时间,消耗内存等,默认为false
-'show_included_files'=>false,//是否显示本次程序运行加载了哪些文件,默认为false
-'error_handler'=>false,//是否接管程序错误,将程序错误显示在console中,默认为false
-'force_client_id'=>'',//日志强制记录到配置的client_id,默认为空
-'allow_client_ids'=>array()//限制允许读取日志的client_id,默认为空,表示所有人都可以获得日志。
+    'host'                => 'localhost',//websocket服务器地址,默认localhost
+    'optimize'            => false,//是否显示利于优化的参数,如果运行时间,消耗内存等,默认为false
+    'show_included_files' => false,//是否显示本次程序运行加载了哪些文件,默认为false
+    'error_handler'       => false,//是否接管程序错误,将程序错误显示在console中,默认为false
+    'force_client_ids'    => array(//日志强制记录到配置的client_id,默认为空,client_id必须在allow_client_ids中
+        //'client_01',
+        //'client_02',
+    ), 
+    'allow_client_ids'    => array(//限制允许读取日志的client_id,默认为空,表示所有人都可以获得日志。
+        //'client_01',
+        //'client_02',
+        //'client_03',
+    ), 
 ),'config');
 
 //输出日志

+ 30 - 10
php/slog.php

@@ -10,6 +10,8 @@ class Slog
     public static $start_memory=0;
     public static $port=1116;//SocketLog 服务的http的端口号
     public static $log_types=array('log','info','error','warn','table','group','groupCollapsed','groupEnd','alert');
+    
+    protected static $_allowForceClientIds = array();    //配置强制推送且被授权的client_id
 
     protected static $_instance;
 
@@ -21,7 +23,7 @@ class Slog
         'show_included_files'=>false,
         'error_handler'=>false,
         //日志强制记录到配置的client_id
-        'force_client_id'=>'',
+        'force_client_ids'=>array(),
         //限制允许读取日志的client_id
         'allow_client_ids'=>array()
     );
@@ -256,7 +258,7 @@ class Slog
         }
         $tabid=self::getClientArg('tabid');
          //是否记录日志的检查
-        if(!$tabid && !self::getConfig('force_client_id'))
+        if(!$tabid && !self::getConfig('force_client_ids'))
         {
             return false;
         }
@@ -264,7 +266,9 @@ class Slog
         $allow_client_ids=self::getConfig('allow_client_ids');
         if(!empty($allow_client_ids))
         {
-            if (!$tabid && in_array(self::getConfig('force_client_id'), $allow_client_ids)) {
+            //通过数组交集得出授权强制推送的client_id
+            self::$_allowForceClientIds = array_intersect((array)$allow_client_ids, (array)self::getConfig('force_client_ids'));
+            if (!$tabid && count(self::$_allowForceClientIds)) {
                 return true;
             }
 
@@ -442,20 +446,36 @@ class Slog
         {
             $client_id='';
         }
-        if($force_client_id=self::getConfig('force_client_id'))
+        if(!empty(self::$_allowForceClientIds))
         {
-            $client_id=$force_client_id;
+            //强制推送到多个client_id
+            foreach(self::$_allowForceClientIds as $force_client_id) {
+                $client_id=$force_client_id;
+                self::sendToClient($tabid, $client_id, self::$logs, $force_client_id);
+            }
+        } else {
+            self::sendToClient($tabid, $client_id, self::$logs, '');
         }
-        $logs=array(
+    }
+
+    /**
+     * 发送给指定客户端
+     * @author Zjmainstay
+     * @param $tabid
+     * @param $client_id
+     * @param $logs
+     * @param $force_client_id
+     */
+    protected static function sendToClient($tabid, $client_id, $logs, $force_client_id) {
+         $logs=array(
             'tabid'=>$tabid,
             'client_id'=>$client_id,
-            'logs'=>self::$logs,
+            'logs'=>$logs,
             'force_client_id'=>$force_client_id,
-        );
+        ); 
         $msg=@json_encode($logs);
         $address='/'.$client_id; //将client_id作为地址, server端通过地址判断将日志发布给谁
-        self::send(self::getConfig('host'),$msg,$address);
-
+        self::send(self::getConfig('host'),$msg,$address); 
     }
 
     public function __destruct()