Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS 

[Ruby] Script/bot to send MediaWiki Recent Changes over UDP to a Campfire room

Requires the Tinder gem, and PHP on the server running MediaWiki must be compiled with --enable-sockets. The computer running the bot will also have to be open to the web - it can't be behind a firewall.

First, add this code to your MediaWiki LocalSettings.php, replacing the IP address with the IP of the server/computer running the bot, and the port to whatever you want to use (probably something above 40,000, but it's up to you):
$wgRC2UDPAddress = '69.178.6.244';
$wgRC2UDPPort = '41895';
$wgRC2UDPPrefix = "";


Now, you can run the bot! This is extremely hackish, because the retarded collective-mailing-list-coding system of the MediaWiki repos has somehow managed to code IRC color codes directly into their exported strings (at least with MediaWiki 1.2alpha, which is what I'm running right now).
You'll need to replace the new tinder definition with your subdomain on Campfire, the username and password with ones for the bot account you've created on your campfire room, and the room name with the room you want the updates to be sent to.
require 'rubygems'
require 'tinder'
require 'socket'
puts 'Dependencies loaded...'

campfire = Tinder::Campfire.new 'subdomain'
campfire.login 'email@domain.com', 'password'
exit unless room = campfire.find_room_by_name('Case Sensitive Room Name')
puts 'Campfire logged in...'

server = UDPSocket.new
exit unless server.bind('0.0.0.0', 41895)
puts 'Socket listening...'

loop do
  msg = server.recv(2048).gsub(/\003[\d]{0,2}/,'').chomp
  print "sending to room: #{msg.inspect}..."
  exit unless room.speak msg.to_s
  puts ' sent!'
end

disable SQL / MySQL in rails logging in development mode

// description of your code here

ActiveRecord::Base.logger = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}_database.log")



goes at end of config/env

Log4j 101

// Once you have included the Log4j jar into the classpath of your
// application, you can use the following code within each class to
// get a logger for that class. That is, all log messages issued from
// within that class will be tagged with the classname to make it
// easier to locate a problem. See my tutorial on Log4j,
// Log4j in 30 Minutes or Less (http://www.johnmunsch.com/projects/Presentations/)
// for a better introduction.

private static Logger log = Logger.getLogger(<class name>.class);


// When you need to specify the location (or a different name) for
// the log.properties file, you can set a specific System variable
// which Log4j will be guaranteed to read as soon as any part of
// your code tries to use it. You do that by passing the following
// to the JVM as you start your Java application.
-Dlog4j.configuration=file:<path to log.properties file>

Flex/ActionScript: Making trace() display the method it was called in

While trace() is useful, its indiscriminate use by me often crowds
the console of my fellow developers. We agreed on making every trace() show
the method it came from, but I am too lazy to do this, this makes it automatic:

public static function TRACE(s:Object):void {
  try {
    throw new Error();
  } catch (e:Error) {
    var stack:String = e.getStackTrace();
    var frames:Array = stack.split("\n");
    var myFrame:String = String(frames[2]);
    myFrame = myFrame.replace("\t", "");
  
    // "at " can be followed by some part of the package
    // you don't want to see. E.g., if your code is all in
    // com.foo.bar, you can put "at com.foo.bar." so as not
    // to crowd the display
    myFrame = myFrame.substr("at ".length);
    myFrame = myFrame.substring(0, myFrame.indexOf("["));
    trace(myFrame + ": " + s);
  }
}

Tagging Your Test Cases

From http://www.railtie.net/articles/2006/09/17/tagging-your-test-cases:
Here's a quick way to follow your tests a little more closely in your test.log. In your test setup, add the following line:
def setup
  RAILS_DEFAULT_LOGGER.debug "\n\e[0;31mRUNNING TEST CASE: #{name}\e[m\n"
end

Saving time spent on page to web server's log file

// Saving time spent on page to web server's log file

(function(){
  var start = new Date;
  window.onunload = function(){
      var time = (new Date - start );
      var image = new Image;
      image.src = "/dummy?t=" + time + "&url=" + document.location;
  }
})();

Setting up basic logging

import logging
logger = logging.getLogger('app')
handler = logging.FileHandler('app.log')
formatter = logging.Formatter('%(asctime)s %(levelname)5s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

Dynamically Enable/Disable WebObjects SQL Logging

Sometimes you want to log generated SQL selectively, so popping -DEOAdaptorDebugEnabled=true on the command line of your app is verbose overkill. Here's how you can selectively enable and disable SQL logging:

NSLog.allowDebugLoggingForGroups(0x10000L);
// Do something that roundtrips the DB, like editingContext.saveChanges().
NSLog.refuseDebugLoggingForGroups(0x10000L);
« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS