/* * Copyright (C) 2006 Astrient Labs, LLC Licensed under the Apache License, * Version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * Astrient Labs, LLC * www.astrientlabs.com * rashid@astrientlabs.com * Rashid Mayes 2006 */ package com.astrientlabs.log; import java.io.DataInputStream; import java.io.DataOutputStream; import java.util.Calendar; import java.util.Vector; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import com.astrientlabs.MediaStreamMidlet; public class Logger implements Runnable { private static int sequenceNumberMajor = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); private static int sequenceNumberMinor; private int flush; private int protocolID = 1; private String applicationName; private String APIKey; private Semaphore semaphore = new Semaphore(0); private Vector messages = new Vector(); private Thread thread = null; public Logger(int flush, String applicationName, String APIKey) { this.flush = flush; this.applicationName = applicationName; this.APIKey = APIKey; (thread = new Thread(this)).start(); } public void log(Throwable t) { t.printStackTrace(); log(t.getClass().getName() + " " + t.getMessage()); } public void log(String message) { messages.addElement(new LogMessage(message)); if ( messages.size() > flush ) { semaphore.release(); } } public void run() { while ( thread != null ) { semaphore.acquire(); uploadMessages(); } } protected void uploadMessages() { HttpConnection c = null; DataInputStream dis = null; DataOutputStream dos = null; String url = MediaStreamMidlet.instance.getAppProperty("server"); if ( url == null ) url = "http://www.hostj2me.com/"; url += "applog"; try { int count = messages.size(); c = (HttpConnection) Connector.open(url,Connector.READ_WRITE); c.setRequestMethod(HttpConnection.POST); dos = c.openDataOutputStream(); dos.writeLong(protocolID); dos.writeUTF(APIKey); dos.writeUTF(applicationName); dos.writeInt(count); LogMessage message; for (int i = 0; i < count; i++ ) { System.out.println("Count " + i); message = (LogMessage)messages.firstElement(); messages.removeElementAt(0); dos.writeLong(message.timeStamp); dos.writeUTF("SEQN: " + sequenceNumberMajor + "." + (sequenceNumberMinor++)); dos.writeUTF(message.message); } dos.flush(); dis = c.openDataInputStream(); int rc = dis.readInt(); //System.out.println(rc); } catch (Exception e) { e.printStackTrace(); } finally { if (dis != null) { try { dis.close(); } catch (Exception e) { } } if (c != null) { try { c.close(); } catch (Exception e) { } } } } public void stop() { semaphore.release(); thread = null; } } class LogMessage { public long timeStamp = System.currentTimeMillis(); public String message; public LogMessage(String message) { this.message = message; } } class Semaphore { private int count = 0; public Semaphore(int count) { this.count = count; } public synchronized void acquire() { while (count == 0) { try { wait(); } catch (InterruptedException e) { } } count--; } public synchronized void release() { count++; notify(); } }