[Title] modify about graceful shutdown in abnormal case, modify skin logger
authorSon Hyunjun <hj79.son@samsung.com>
Thu, 12 Apr 2012 06:51:05 +0000 (15:51 +0900)
committerSon Hyunjun <hj79.son@samsung.com>
Thu, 12 Apr 2012 06:51:05 +0000 (15:51 +0900)
[Type] Enhancement
[Module] Skin
[Priority] Minor
[CQ#]
[Redmine#]
[Problem]
[Cause]
[Solution]

Change-Id: I2b8902f1bc1033f9c828789eddb269347c1b989e

tizen/src/skin/client/src/org/tizen/emulator/skin/EmulatorSkinMain.java
tizen/src/skin/client/src/org/tizen/emulator/skin/comm/sock/SocketCommunicator.java
tizen/src/skin/client/src/org/tizen/emulator/skin/log/SkinLogger.java
tizen/src/skin/client/src/org/tizen/emulator/skin/screenshot/ScreenShotDialog.java
tizen/src/skin/maruskin_server.c

index 2eda2c2ac8a0450313b28591cc038fc17108fbcc..4dfb0050b16dedd3edd389f4fdf94fccf9ffc433 100644 (file)
@@ -74,14 +74,22 @@ public class EmulatorSkinMain {
                
                try {
 
-                       Map<String, String> argsMap = parsArgs( args );
+                       String vmPath = getVmPath( args );
+                       if ( StringUtil.isEmpty( vmPath ) ) {
+                               throw new IllegalArgumentException( ArgsConstants.VM_PATH + " in arguments is null." );
+                       }
+
+                       SkinLogger.init( SkinLogLevel.DEBUG, vmPath );
 
-                       String vmPath = (String) argsMap.get( ArgsConstants.VM_PATH );
+                       logger = SkinLogger.getSkinLogger( EmulatorSkinMain.class ).getLogger();
+                       logger.info( "!!! Start Emualtor Skin !!!" );
+                       
+                       Map<String, String> argsMap = parsArgs( args );
                        
                        String skinPropFilePath = vmPath + File.separator + SKIN_PROPERTIES_FILE_NAME;
                        Properties skinProperties = loadProperties( skinPropFilePath, true );
                        if ( null == skinProperties ) {
-                               System.err.println( "[SkinLog]Fail to load skin properties file." );
+                               logger.severe( "Fail to load skin properties file." );
                                System.exit( -1 );
                        }
 
@@ -203,39 +211,50 @@ public class EmulatorSkinMain {
                                break;
                        }
                }
-               
-               String vmPath = argsMap.get( ArgsConstants.VM_PATH );
-               
-               SkinLogger.init( skinLogLevel, vmPath );
-               
-               logger = SkinLogger.getSkinLogger( EmulatorSkinMain.class ).getLogger();
+
+               SkinLogger.setLevel( skinLogLevel.level() );
                
        }
 
+       private static String getVmPath( String[] args ) {
+
+               for ( int i = 0; i < args.length; i++ ) {
+                       String arg = args[i];
+                       String[] split = arg.split( "=" );
+                       if ( 1 < split.length ) {
+                               if ( ArgsConstants.VM_PATH.equals( split[0].trim() ) ) {
+                                       return split[1].trim();
+                               }
+                       }
+               }
+
+               return null;
+
+       }
+       
        private static Map<String, String> parsArgs( String[] args ) {
 
                Map<String, String> map = new HashMap<String, String>();
 
                for ( int i = 0; i < args.length; i++ ) {
                        String arg = args[i];
-                       System.out.println( "[SkinLog]arg[" + i + "] " + arg );
+                       logger.info( "arg[" + i + "] " + arg );
                        String[] split = arg.split( "=" );
 
                        if ( 1 < split.length ) {
 
                                String argKey = split[0].trim();
                                String argValue = split[1].trim();
-                               System.out.println( "[SkinLog]argKey:" + argKey + "  argValue:" + argValue );
                                map.put( argKey, argValue );
 
                        } else {
-                               System.out.println( "[SkinLog]only one argv:" + arg );
+                               logger.info( "sinlge argv:" + arg );
                        }
                }
 
-               System.out.println( "[SkinLog]========================================" );
-               System.out.println( "[SkinLog]args:" + map );
-               System.out.println( "[SkinLog]========================================" );
+               logger.info( "================= argsMap =====================" );
+               logger.info( map.toString() );
+               logger.info( "===============================================" );
 
                return map;
 
@@ -283,7 +302,7 @@ public class EmulatorSkinMain {
                                
                                if ( !file.exists() ) {
                                        if ( !file.createNewFile() ) {
-                                               System.err.println( "[SkinLog]Fail to create new " + filePath + " property file." );
+                                               logger.severe( "Fail to create new " + filePath + " property file." );
                                                return null;
                                        }
                                }
@@ -300,14 +319,13 @@ public class EmulatorSkinMain {
                                        properties = new Properties();
                                        properties.load( fis );
                                }
-                               
+
                        }
 
-                       System.out.println( "[SkinLog]load properties file : " + filePath );
+                       logger.info( "load properties file : " + filePath );
 
                } catch ( IOException e ) {
-                       System.err.println( "[SkinLog]Fail to load skin properties file." );
-                       e.printStackTrace();
+                       logger.log( Level.SEVERE, "Fail to load skin properties file.", e );
                } finally {
                        IOUtil.close( fis );
                }
index 11eee120328e90f8b937c2ae36822f76c4c82d27..b0e1f8533074ebb0cdb5733f69435615732ffd5f 100644 (file)
@@ -262,7 +262,7 @@ public class SocketCommunicator implements ICommunicator {
                                        break;
                                }
                                default: {
-                                       logger.warning( "Unknown command from QEMU. command:" + cmd );
+                                       logger.severe( "Unknown command from QEMU. command:" + cmd );
                                        break;
                                }
                                }
@@ -356,8 +356,7 @@ public class SocketCommunicator implements ICommunicator {
 
                        if ( 0 < length ) {
                                if ( logger.isLoggable( Level.FINE ) ) {
-                                       logger.fine( "== data ==" );
-                                       logger.fine( data.toString() );
+                                       logger.fine( "[Socket] data  - " + data.toString() );
                                }
                        }
 
@@ -411,4 +410,4 @@ public class SocketCommunicator implements ICommunicator {
                }
        }
 
-}
\ No newline at end of file
+}
index 4b0447b120b3e00241607efd907fcd9e33f06f6d..d5c79fd13596d43431a29a90f458d9bf1b068d41 100644 (file)
@@ -31,13 +31,18 @@ package org.tizen.emulator.skin.log;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.logging.FileHandler;
+import java.util.logging.Formatter;
 import java.util.logging.Level;
+import java.util.logging.LogRecord;
 import java.util.logging.Logger;
-import java.util.logging.SimpleFormatter;
 
 import org.tizen.emulator.skin.util.StringUtil;
 
@@ -85,6 +90,13 @@ public class SkinLogger {
        public Logger getLogger() {
                return this.logger;
        }
+
+       public static void setLevel( Level level ) {
+               if( null != fileHandler ) {
+                       fileHandler.setLevel( level );
+               }
+       }
+
        
        public static void init( SkinLogLevel logLevel, String filePath ) {
                
@@ -117,7 +129,6 @@ public class SkinLogger {
                        }
 
                        try {
-                               System.out.println( "[SkinLog]log file path:" + file.getAbsolutePath() );
                                fileHandler = new FileHandler( file.getAbsolutePath(), false );
                        } catch ( SecurityException e1 ) {
                                e1.printStackTrace();
@@ -133,13 +144,9 @@ public class SkinLogger {
                                e.printStackTrace();
                        }
                        
-                       SimpleFormatter simpleFormatter = new SimpleFormatter();
-                       fileHandler.setFormatter( simpleFormatter );
-                       
+                       fileHandler.setFormatter( new SkinFormatter() );
                        fileHandler.setLevel( logLevel.level() );
                        
-                       System.out.println( "[SkinLog]SkinLogger level:" + logLevel.value() );
-                       
                }
                
        }
@@ -178,4 +185,64 @@ public class SkinLogger {
                
        }
 
-}
\ No newline at end of file
+}
+
+class SkinFormatter extends Formatter {
+
+       private SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyyMMdd-HHmmss" );
+       private String lineSeparator = System.getProperty( "line.separator" );
+
+       @Override
+       public String format( LogRecord record ) {
+
+               StringBuilder builder = new StringBuilder();
+
+               builder.append( "[" );
+               builder.append( record.getLevel().toString() );
+               builder.append( ":" );
+
+               String formattedDate = simpleDateFormat.format( new Date( record.getMillis() ) );
+               builder.append( formattedDate );
+               builder.append( ":" );
+
+               if ( null != record.getSourceClassName() ) {
+                       String sourceClassName = record.getSourceClassName();
+                       String[] split = sourceClassName.split( "\\." );
+                       builder.append( split[split.length - 1] );
+               } else {
+                       builder.append( record.getLoggerName() );
+               }
+               
+               if ( null != record.getSourceMethodName() ) {
+                       builder.append( "." );
+                       builder.append( record.getSourceMethodName() );
+               }
+
+               builder.append( "] " );
+
+               String message = formatMessage( record );
+               builder.append( message );
+
+               builder.append( lineSeparator );
+
+               if ( null != record.getThrown() ) {
+                       
+                       try {
+                               
+                               StringWriter sw = new StringWriter();
+                               PrintWriter pw = new PrintWriter( sw );
+                               record.getThrown().printStackTrace( pw );
+                               pw.close();
+                               
+                               builder.append( sw.toString() );
+                               
+                       } catch ( Exception ex ) {
+                       }
+               }
+
+               return builder.toString();
+
+       }
+
+}
+       
index ceca7ea171f246b54d624636f58397ef633804c4..a0b232411be6345fe59f3272184619cd7caadef2 100644 (file)
@@ -151,7 +151,7 @@ public class ScreenShotDialog {
                        @Override
                        public void paintControl( PaintEvent e ) {
 
-                               logger.info( "paint image." );
+                               logger.fine( "paint image." );
 
                                if ( null != image && !image.isDisposed() ) {
                                        e.gc.drawImage( image, CANVAS_MARGIN, CANVAS_MARGIN );
index 9b1fdc8f9b3da85747e728dca3323cc23e45f3eb..b3277ac99567333ce291b2128d5b509a88c564e5 100644 (file)
@@ -159,61 +159,59 @@ void shutdown_skin_server( void ) {
 
     INFO( "shutdown_skin_server\n" );
 
-    if ( client_sock ) {
+    int close_server_socket = 0;
+    int success_send = 0;
 
+    if ( client_sock ) {
         INFO( "send shutdown to skin.\n" );
-
         if ( 0 > send_skin_header_only( client_sock, SEND_SHUTDOWN, 1 ) ) {
-
             ERR( "fail to send SEND_SHUTDOWN to skin.\n" );
-            // force close
-            is_force_close_client = 1;
-#ifdef _WIN32
-            closesocket( client_sock );
-#else
-            close( client_sock );
-#endif
-
+            close_server_socket = 1;
         } else {
+            success_send = 1;
             // skin sent RECV_RESPONSE_SHUTDOWN.
         }
     }
 
-    if ( client_sock ) {
+    if( success_send ) {
+
+        int count = 0;
+        int max_sleep_count = 10;
+
+        while ( 1 ) {
+
+            if ( max_sleep_count < count ) {
+                close_server_socket = 1;
+                break;
+            }
+
+            if ( stop_server ) {
+                INFO( "skin client sent normal shutdown response.\n" );
+                break;
+            } else {
 #ifdef _WIN32
-        closesocket( client_sock );
+                Sleep( 1 ); // 1ms
 #else
-        close( client_sock );
+                usleep( 1000 ); // 1ms
 #endif
+                count++;
+            }
+        }
     }
 
-    int close_server_socket = 0;
-    int count = 0;
-    int max_sleep_count = 10;
-
-    while( 1 ) {
-
-        if( max_sleep_count < count ) {
-            close_server_socket = 1;
-            break;
-        }
+    stop_server = 1;
+    is_force_close_client = 1;
 
-        if( stop_server ) {
-            INFO( "skin client sent normal shutdown response.\n" );
-            break;
-        }else {
+    if ( client_sock ) {
 #ifdef _WIN32
-            Sleep( 1 ); // 1ms
+        closesocket( client_sock );
 #else
-            usleep( 1000 ); // 1ms
+        close( client_sock );
 #endif
-            count++;
-        }
     }
 
     if ( close_server_socket ) {
         WARN( "skin client did not send normal shutdown response.\n" );
-        stop_server = 1;
         if ( server_sock ) {
 #ifdef _WIN32
             closesocket( server_sock );
@@ -1023,13 +1021,11 @@ static void* do_heart_beat( void* args ) {
 
                     is_force_close_client = 1;
                     if ( client_sock ) {
-                        if ( client_sock ) {
 #ifdef _WIN32
-                            closesocket( client_sock );
+                        closesocket( client_sock );
 #else
-                            close( client_sock );
+                        close( client_sock );
 #endif
-                        }
                     }
 
                     start_skin_client( skin_argc, skin_argv );
@@ -1048,17 +1044,14 @@ static void* do_heart_beat( void* args ) {
 
         is_force_close_client = 1;
         if ( client_sock ) {
-            if ( client_sock ) {
 #ifdef _WIN32
-                closesocket( client_sock );
+            closesocket( client_sock );
 #else
-                close( client_sock );
+            close( client_sock );
 #endif
-            }
         }
 
         stop_server = 1;
-
         if ( server_sock ) {
 #ifdef _WIN32
             closesocket( server_sock );