Skip to content

Instantly share code, notes, and snippets.

@creativepsyco
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creativepsyco/166feac5376cc25ba63c to your computer and use it in GitHub Desktop.
Save creativepsyco/166feac5376cc25ba63c to your computer and use it in GitHub Desktop.
This script logs data to PaperTrail. Do change your port. CommonLoop is attached which allows for Requests to be scheduled on a single executor thread
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class CommonEventLoop {
private static CommonEventLoop __instance;
private ScheduledExecutorService m_executor = Executors.newSingleThreadScheduledExecutor();
public static CommonEventLoop getInstance() {
if (__instance == null)
__instance = new CommonEventLoop();
return __instance;
}
public void post(Runnable call) {
m_executor.execute(call);
}
public void cancel(Future f) {
if (f.isCancelled()) {
return;
}
if (f.isDone()) {
return;
}
f.cancel(true);
}
public Future delayPost(Runnable call, int nMillSec) {
return m_executor.schedule(call, nMillSec, TimeUnit.MILLISECONDS);
}
public void shutdown() {
m_executor.shutdown();
}
}
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Created by msk
* Date: 7/14/14
*/
public class SimpleDatagramSocket {
static final String LOG_FORMATTER = "<22>1 %s %s %s - - - %s";
static final String LOG_FORMATTER_RFC3164 = "<22>%s %s %s: %s";
static final String LOG_FORMATTER2 = "<22>1 2014-06-18T09:56:21Z sendername programname - - - the log message";
static final String paperTrailHost = "logs.papertrailapp.com";
static final String LOG_TAG = "Papertrail";
static int port = 15643514263; // Override
public static void sendLog(final String tag, final String message) {
LoggerTask task = new LoggerTask(tag, message);
/* Post it to a thread */
// Do something like this:
CommonEventLoop.getInstance().post(task);
}
private static String getDateTimeInISO8601() {
/*
* I just do this to avoid an issue since Java 6 is being used
* Java 6 has no ISO8601 support >.<
* For an alternative check: http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date
*/
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(tz);
return df.format(new Date());
}
/*
* Helper class to post Runnable tasks
* To upload Papertrail logs
*/
private static class LoggerTask implements Runnable {
private String tag;
private String message;
private String finalMessage;
private LoggerTask(String tag, String message) {
this.tag = tag;
this.message = message;
this.finalMessage = String.format(Locale.ENGLISH, LOG_FORMATTER, getDateTimeInISO8601(), "Android", tag, message);
}
@Override
public void run() {
byte[] buffer;
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket();
/* Create a custom packet */
/*buffer = toByteArray();*/
InetAddress address = InetAddress.getByName(paperTrailHost);
Log.d(LOG_TAG, "Sending Datagram Packet");
buffer = finalMessage.getBytes("UTF-8");
DatagramPacket packet = new DatagramPacket(
buffer, buffer.length, address, port);
datagramSocket.send(packet);
Log.d(LOG_TAG, "Datagram packet sent");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (datagramSocket != null)
datagramSocket.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment