platform/bb/RubyVM/src/com/rho/RhoConf.java in rhodes-2.1.0 vs platform/bb/RubyVM/src/com/rho/RhoConf.java in rhodes-2.2.0.beta.1

- old
+ new

@@ -3,11 +3,11 @@ import java.io.IOException; import java.util.Enumeration; import java.util.Hashtable; import com.rho.net.NetResponse; -import com.xruby.runtime.builtin.ObjectFactory; +import com.xruby.runtime.builtin.*; import com.xruby.runtime.lang.*; import com.rho.net.NetRequest.MultipartItem; import com.rho.file.SimpleFile; public class RhoConf { @@ -273,10 +273,79 @@ }catch(Exception exc){ } } + private static long readToBuffer(java.io.InputStream is, byte[] buf, StringBuffer res, long limit)throws Exception + { + long nTotal = 0; + while(nTotal < limit) + { + long nRead = is.read(buf); + if ( nRead < 0 ) + break; + if ( nTotal + nRead > limit ) + nRead = limit - nTotal; + + nTotal += nRead; + res.append(new String(buf,0,(int)nRead)); + } + + return nTotal; + } + + static RubyString getLogText_ruby(long limit)throws Exception + { + StringBuffer res = new StringBuffer(); + SimpleFile oFile = null; + RhoLogConf logConf = RhoLogger.getLogConf(); + boolean bOldSaveToFile = logConf.isLogToFile(); + logConf.setLogToFile(false); + java.io.InputStream is = null; + try{ + oFile = RhoClassFactory.createFile(); + oFile.open( logConf.getLogFilePath(), true, false); + + if ( oFile.isOpened() ) + { + long nFileSize = oFile.length(); + long nPos = logConf.getLogTextPos(); + long nMaxSize = nFileSize > nPos ? nFileSize : nPos; + if ( limit <= 0 || limit > nMaxSize) + limit = nMaxSize; + + res = new StringBuffer((int)limit); + is = oFile.getInputStream(); + byte[] buf = new byte[8096]; + if ( limit <= nPos ) + { + is.skip(nPos-limit); + readToBuffer(is, buf, res, limit); + }else + { + is.skip(nFileSize-(limit-nPos)); + long nRead = readToBuffer(is, buf, res, limit); + + oFile.close(); + oFile.open( logConf.getLogFilePath(), true, false); + is = oFile.getInputStream(); + readToBuffer(is, buf, res, limit-nRead); + } + + } + + }finally + { + if ( oFile != null ) + try{ oFile.close(); }catch(IOException exc2){} + + logConf.setLogToFile(bOldSaveToFile); + } + + return ObjectFactory.createString(res); + } + public static boolean sendLog() { com.rho.net.NetRequest nq = RhoClassFactory.createNetRequest(); String strDevicePin = ""; String strClientID = ""; @@ -391,9 +460,47 @@ sendLog(); return RubyConstant.QNIL; }catch(Exception e) { LOG.ERROR("send_log failed", e); + throw (e instanceof RubyException ? (RubyException)e : new RubyException(e.getMessage())); + } + + } + }); + + klass.getSingletonClass().defineMethod("clean_log", new RubyNoArgMethod() { + protected RubyValue run(RubyValue receiver, RubyBlock block) { + try{ + RhoLogger.clearLog(); + return RubyConstant.QNIL; + }catch(Exception e) + { + LOG.ERROR("clean_log failed", e); + throw (e instanceof RubyException ? (RubyException)e : new RubyException(e.getMessage())); + } + + } + }); + + klass.getSingletonClass().defineMethod("read_log", new RubyNoOrOneArgMethod() { + protected RubyValue run(RubyValue receiver, RubyBlock block) { + try{ + return getLogText_ruby(0); + }catch(Exception e) + { + LOG.ERROR("read_log failed", e); + throw (e instanceof RubyException ? (RubyException)e : new RubyException(e.getMessage())); + } + + } + + protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) { + try{ + return getLogText_ruby(arg.toInt()); + }catch(Exception e) + { + LOG.ERROR("read_log failed", e); throw (e instanceof RubyException ? (RubyException)e : new RubyException(e.getMessage())); } } });