Also, very often, you can show a loss when you have played solid poker, even for consecutive sessions, so you shouldn't care about losing money and thus leaving unless:
1) You're on tilt (if you've lost 30bb playing limit and are steaming, even if the game is good, leave)
2) You've lost so much that you're not playing at the right stakes now (took a shot, chased a loss, etc.)
In these cases you should leave immediatly and take a break. The problem for me is not so much when to leave, but when to play. Very often I'll want to play, but I'll be tired (I work very long hours and don't have a lot of days off). Since I really play my worst poker when tired so I just study my previous hands or watch a table and take note on players instead. I'll usually only sit at the table when fully rested with my concentration and leave when too tired, bored or when the table has changed. In short, you should leave when you feel you are not at your best.